2013-04-11 2 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

Где говорится, что синтаксическая ошибка есть? Пожалуйста, сузите его до строки, которая, по ее словам, вызывает проблему. –

+0

Что такое 'print()'? –

+0

@ColeJohnson 'print()' просто создает пустую строку. Это было бы эквивалентно произношению 'print (" ")'. Вероятно, он хотел просто открыть пробел в выводе программы. – erdekhayser

ответ

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, в сочетании с """ тройной Qouted строку для форматирования текста за один раз, форматирование значений float с двумя десятичными знаками после десятичной точки.

выход

Пример:

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

0

квотирования вы используете может быть сложно, поэтому вы должны лучше попытаться отформатировать параметры печати, что-то вроде этого sould улучшить немного:

# 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

Смежные вопросы