Python语法错误没有显示?

时间:2015-03-14 06:55:49

标签: python

import sys
import time
print ("Hello Eric the builder")
print ("This program will calculate how many bricks are required to build an igloo")
# Start, print brick types
start = input("Enter YES to start: ")
if start.lower() == "yes" :
    print ("(1) Budget - length: 45cm x height: 25cm x depth: 15cm")
    print ("(2) Superior - length: 35cm x height: 25cm x depth: 20cm")
    print ("(3) Luxury - length: 30cm x height: 20cm x depth: 25cm")

selection = input("Select your brick type")


# When selection has been made
if input == "budget".lower() or "1":
    print ("\nYou have selected (1) Budget ")
    print ("Please note tunnel radius is 45cm(fixed)")
    tunnel1 = int(input("Enter in the length of the igloo's tunnel in cm"))
    dome1 = int(input("Enter in the radius of your dome in cm"))
    print ("\n This program will now calculate the number of bricks required")
tunnelarea = pi*tunnel1*45 + pi*45**2 #mathematical calculations
domearea = 2*pi*dome1**2 - 0.5*(45+15)**2
bricksrequired = tunnelarea + domearea /(45*25)
print ("\n The number of brick required is"),bricksrequired


elif input == "superior".lower() or "2":
print ("\nYou have selected (2) Superior")
print ("Please note tunnel radius is 45cm(fixed)")
tunnel1 =  int(input("Enter in the length of your igloo's tunnel in cm"))
dome2 = int(input("Enter in the radius of your dome in cm"))
tunnelarea = pi*tunnel2*45 + pi*45**2
domearea = 2*pi*dome2*2 - 0.5*(45+20)**2 
bricksrequired = tunnelarea + domearea /(35*25)
print ("\n The number of brick required is"),bricksrequired


elif input == "luxury".lower() or "3":
print ("\nYou have selected (3) Luxury")

我尝试运行此程序时遇到语法错误,但它没有显示在哪里。另外,如何在程序完成或限制值后重新启动该程序?在此先感谢,非常感谢

print ("Please note tunnel radius is 45cm(fixed)")
tunnel3 = int(input("Enter in the length of your igloo's tunnel in cm"))
dome3 = int(input("Enter in the radius of your dome in cm"))
tunnelarea = pi*tunnel3*45 + pi*45**2
domearea = 2*pi*dome3*2 - 0.5*(45+25)**2
bricksrequired = tunnelarea + domearea /(30*20)
print ("\n The number of brick required is"),bricksrequired

if dome_radius < 45 :
    print ("\nInvalid input. Please enter a value larger than 45!")

3 个答案:

答案 0 :(得分:0)

你有多个问题。首先,对输入语句使用.lower()而不是字符串。您希望将USER INPUT更改为小写而不是字符串。

你有:

selection = input("Select your brick type")
elif input == "superior".lower() or "2":

你应该拥有什么:

selection = input("Select your brick type").lower()
elif input == "superior" or selection == "2":

答案 1 :(得分:0)

你在多个地方重复了同样的错误,所以让我们从第一个实例开始:

selection = input("Select your brick type")


# When selection has been made
if input == "budget".lower() or "1":

input是函数的名称,您正在将函数与字符串进行比较。您需要比较用户输入的作为该函数的结果,该函数存储在selection

if selection.lower() == '1':

您在此处遇到同样的问题:elif input == "superior".lower() or "2":,此行应为elif selection.lower() == '2':

答案 2 :(得分:-1)

Licharde先生很开心。

# When selection has been made
if str(input).lower() == "budget" or "1":
    print ("\nYou have selected (1) Budget ")
    print ("Please note tunnel radius is 45cm(fixed)")
    tunnel1 = int(input("Enter in the length of the igloo's tunnel in cm"))
    dome1 = int(input("Enter in the radius of your dome in cm"))
    print ("\n This program will now calculate the number of bricks required")
    #mathematical calculations
    tunnelarea = pi*tunnel1*45 + pi*45**2
    domearea = 2*pi*dome1**2 - 0.5*(45+15)**2
    bricksrequired = tunnelarea + domearea /(45*25)
    print ("\n The number of brick required is"),bricksrequired

elif str(input).lower() == "superior" or "2":
    print ("\nYou have selected (2) Superior")
    print ("Please note tunnel radius is 45cm(fixed)")
    tunnel1 =  int(input("Enter in the length of your igloo's tunnel in cm"))
    dome2 = int(input("Enter in the radius of your dome in cm"))
    tunnelarea = pi*tunnel2*45 + pi*45**2
    domearea = 2*pi*dome2*2 - 0.5*(45+20)**2 
    bricksrequired = tunnelarea + domearea /(35*25)
    print ("\n The number of brick required is"),bricksrequired

原始代码问题的解决方案:

  • 在输入上使用str()将其更改为字符串,以便使用.lower()

if inputif str(input).lower()

  • 从&#34;预算&#34;中删除.lower()和&#34;上级&#34;。真是奇怪的代码。

"budget".lower()"budget"

  • 缩进。删除缩进意味着if不再使用。 elif需要与if正好相联系,换句话说,就在{{1}}之下。因此,原始代码不起作用,并返回语法错误。