Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.3:无效语法错误_Python_Python 3.x_Syntax Error - Fatal编程技术网

Python 3.3:无效语法错误

Python 3.3:无效语法错误,python,python-3.x,syntax-error,Python,Python 3.x,Syntax Error,我不明白为什么这个程序中会出现无效的语法错误。我想这相当简单,但我对这一点很陌生。事情是这样的: # Welcome Message print ("Hello and welcome to the Plesha EasyTube Wizard!") print ("Today we will be gathering measurements from you to determine the \ materials and cost of your tubes.") print ("All

我不明白为什么这个程序中会出现无效的语法错误。我想这相当简单,但我对这一点很陌生。事情是这样的:

# Welcome Message
print ("Hello and welcome to the Plesha EasyTube Wizard!")
print ("Today we will be gathering measurements from you to determine the \
materials and cost of your tubes.")
print ("All measurements should be in centimeters!")
print ()

# Collect user inputs
height = float(input("Let's begin: What is the height of you desired tube? "))
radius = float(input("And what is the radius? "))
count = int(input("How many would you like? "))

# Set Constants
steelPrice = 0.14
rubberPrice = 0.02

# Calculations
import math
singleTubeVol = math.pi * (radius ** 2) * height
allTubeVol = singleTubeVol * count
singleTubeSurface = (2 * math.pi * (radius ** 2)) + (2 * math.pi * radius * height)
allTubeSurface = singleTubeSurface * count
singleTubeRubber = 2 * math.pi * (radius + 0.5) * height
allTubeRubber = singleTubeRubber * count
steelCost = steelPrice * allTubeSurface
rubberCost = rubberPrice * allTubeRubber
totalCost = rubberCost + steelCost

# Output
                                                      V------ here is where the problem is
print ("You wanted ", count " tubes in the dimesions ", height \
   " centimeters by ", radius " centimeters (radius).")
print ("The volume of a single tube of your specifications is: ", singleTubeVol)
print ("The total volume of your tube order will be ", allTubeVol)
print ("You will require ", allTubeSurface " square centimeters of steel. Totalling "\
   , steelCost "in price." )
print ("You will require ", allTubeRubber " square centimeters of rubber. Totalling "\
   , rubberCost " in price." )
print ("Your total cost for this order will be ", totalCost)

非常感谢您对新手的帮助。

您忘了几个逗号:

print ("You wanted ", count, " tubes in the dimesions ", height,
#                     -----^                              -----^
下一行还有更多内容:

   " centimeters by ", radius, " centimeters (radius).")
#                       -----^
print ("The volume of a single tube of your specifications is: ", singleTubeVol)
print ("The total volume of your tube order will be ", allTubeVol)
print ("You will require ", allTubeSurface, " square centimeters of steel. Totalling "
#                                    -----^
   , steelCost, "in price." )
#        -----^
print ("You will require ", allTubeRubber, " square centimeters of rubber. Totalling "
#                                   -----^
   , rubberCost, " in price." )
#         -----^
如果您使用格式,我会更好:

print("""\
You wanted {count} tubes in the dimesions {height:0.2f} centimeters by {radius:0.2f} centimeters (radius).
The volume of a single tube of your specifications is: {singleTubeVol:0.2f}
The total volume of your tube order will be {allTubeVol:0.2f}
You will require {allTubeSurface:0.2f} square centimeters of steel. Totalling {steelCost:0.2f} in price.
You will require {allTubeRubber:0.2f} square centimeters of rubber. Totalling {rubberCost:0.2f} in price.
Your total cost for this order will be {totalCost:0.2f}""".format(**locals()))
这将使用与三重qouted字符串相结合的方式一次性格式化文本,将浮点值格式化为小数点后的两位小数

样本输出:

Hello and welcome to the Plesha EasyTube Wizard!
Today we will be gathering measurements from you to determine the materials and cost of your tubes.
All measurements should be in centimeters!

Let's begin: What is the height of you desired tube? 10
And what is the radius? 2.5
How many would you like? 3
You wanted 3 tubes in the dimesions 10.00 centimeters by 2.50 centimeters (radius).
The volume of a single tube of your specifications is: 196.35
The total volume of your tube order will be 589.05
You will require 589.05 square centimeters of steel. Totalling 82.47 in price.
You will require 565.49 square centimeters of rubber. Totalling 11.31 in price.
Your total cost for this order will be 93.78

您忘记了几个逗号:

print ("You wanted ", count, " tubes in the dimesions ", height,
#                     -----^                              -----^
下一行还有更多内容:

   " centimeters by ", radius, " centimeters (radius).")
#                       -----^
print ("The volume of a single tube of your specifications is: ", singleTubeVol)
print ("The total volume of your tube order will be ", allTubeVol)
print ("You will require ", allTubeSurface, " square centimeters of steel. Totalling "
#                                    -----^
   , steelCost, "in price." )
#        -----^
print ("You will require ", allTubeRubber, " square centimeters of rubber. Totalling "
#                                   -----^
   , rubberCost, " in price." )
#         -----^
如果您使用格式,我会更好:

print("""\
You wanted {count} tubes in the dimesions {height:0.2f} centimeters by {radius:0.2f} centimeters (radius).
The volume of a single tube of your specifications is: {singleTubeVol:0.2f}
The total volume of your tube order will be {allTubeVol:0.2f}
You will require {allTubeSurface:0.2f} square centimeters of steel. Totalling {steelCost:0.2f} in price.
You will require {allTubeRubber:0.2f} square centimeters of rubber. Totalling {rubberCost:0.2f} in price.
Your total cost for this order will be {totalCost:0.2f}""".format(**locals()))
这将使用与三重qouted字符串相结合的方式一次性格式化文本,将浮点值格式化为小数点后的两位小数

样本输出:

Hello and welcome to the Plesha EasyTube Wizard!
Today we will be gathering measurements from you to determine the materials and cost of your tubes.
All measurements should be in centimeters!

Let's begin: What is the height of you desired tube? 10
And what is the radius? 2.5
How many would you like? 3
You wanted 3 tubes in the dimesions 10.00 centimeters by 2.50 centimeters (radius).
The volume of a single tube of your specifications is: 196.35
The total volume of your tube order will be 589.05
You will require 589.05 square centimeters of steel. Totalling 82.47 in price.
You will require 565.49 square centimeters of rubber. Totalling 11.31 in price.
Your total cost for this order will be 93.78

您正在使用的引用可能很棘手,因此您最好尝试格式化打印参数,类似这样的内容应该会有所改进:

 # Output ------ here is where the problem is
print ("You wanted %d tubes in the dimesions %d centimeters by %d centimeters (%d)." %(count, height, radius, radius))
print ("The volume of a single tube of your specifications is: ", singleTubeVol)
print ("The total volume of your tube order will be ", allTubeVol)
print ("You will require %d square centimeters of steel. Totalling %d in price." %(allTubeSurface, steelCost))
print ("You will require %d square centimeters of rubber. Totalling %d in price." %(allTubeRubber, rubberCost) )
print ("Your total cost for this order will be ", totalCost)

不过,我相信还有更好的解决方案。

您使用的报价可能会比较棘手,因此您最好尝试格式化打印参数,类似这样的内容应该会有所改进:

 # Output ------ here is where the problem is
print ("You wanted %d tubes in the dimesions %d centimeters by %d centimeters (%d)." %(count, height, radius, radius))
print ("The volume of a single tube of your specifications is: ", singleTubeVol)
print ("The total volume of your tube order will be ", allTubeVol)
print ("You will require %d square centimeters of steel. Totalling %d in price." %(allTubeSurface, steelCost))
print ("You will require %d square centimeters of rubber. Totalling %d in price." %(allTubeRubber, rubberCost) )
print ("Your total cost for this order will be ", totalCost)

不过,我相信还有更好的解决方案。

它说语法错误在哪里?请将范围缩小到它所说的引起问题的那一行。@ColeJohnson
print()
只是创建了一个空行。这相当于说
print(“”
)。他可能只是想在程序的输出中打开一个空格。它说语法错误在哪里?请将范围缩小到它所说的引起问题的那一行。@ColeJohnson
print()
只是创建了一个空行。这相当于说
print(“”
)。他可能只是想在程序的输出中打开一个空格,我最初就是这样做的。我正在完成学业,显然对于我参加的课程来说,这种形式是不被鼓励的。有趣的是,它都以这种格式正确打印,所以我希望我能使用它。这就是我最初使用它的方式。我正在完成学业,显然对于我参加的课程来说,这种形式是不被鼓励的。有趣的是,它以这种格式正确打印,所以我希望我能使用它。谢谢,这就是问题所在。实际上需要很多逗号。我很困惑,因为它指向了我。谢谢你,这就是问题所在。实际上需要很多逗号。我很困惑,因为它指向了我。