2013-04-11 106 views
0

我很困惑,为什么我在这个程序中得到一个无效的语法错误。我想这很简单,但我对此很陌生。这是整个事情:Python 3.3:无效的语法错误

# 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) 

我欣赏任何帮助newb。

+1

它说的语法错误是什么?请缩小到它所说的导致问题的路线。 – 2013-04-11 19:34:20

+0

什么是'print()'? – 2013-04-11 19:57:44

+0

@ColeJohnson'print()'只是创建一个空行。这相当于说'print(“”)'。他可能想在程序的输出中打开一个空格。 – erdekhayser 2013-04-11 20:10:56

回答

4

你忘了几个逗号:

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())) 

这使用str.format() method,与"""结合使用三重字符串来格式化文本一次性使用小数点后两位小数格式化浮点值。

输出示例:

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 
+0

谢谢,这是问题所在。实际上,需要很多逗号。我很困惑,因为它指向我。 – TheZoetrope 2013-04-11 20:20:24

0

的您正在使用可能会非常棘手,所以你最好尝试打印格式参数报价,这样的改进前人的精力一点点:

# 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) 

我仍然相信有更好的解决方案。

+0

这是我最初的样子。我正在上学,显然对于我采用这种格式的班而言,我感到灰心。有趣的是,这是所有打印正确的格式,所以我希望我可以使用它。 – TheZoetrope 2013-04-11 20:17:43