我正在制作一个毕达哥拉斯计算器,但我一直在收到相同的信息"语法无效" 我是编码的新手,所以我只创建了我认为应该告诉用户缺少的一面,如果你能向我解释我做错了什么,我将非常感激。消息说"语法无效"如果x == 1'
,则突出显示第一个冒号# This should ask the user to pick an option
menu="Pythagoras Calculator:\n\
1. Find a hypotenuse?\n\
2. Find another side?\n\
9. Exit\n"
x = int(input(menu))
if x == 1:
a = int(input("what is the other side?(cm)"))
b = int(input("what is the last side?(cm)"))
import math
print("Hypotenuse=",math.sqrt((a**2)+(b**2)))
elif x == 2:
c = int(input("what is the hypotenuse?(cm)"))
d = int(input("what is the otherside?(cm)"))
import math
print("Other side=",math.sqrt((c**2)-(d**2)))
elif x == 9:
print("Good Bye")
答案 0 :(得分:2)
尝试将结束括号添加到x = int(输入(菜单)行
)所以它应该是x = int(输入(菜单))
答案 1 :(得分:0)
由于您不熟悉编码,我认为您可能想要更详细一些代码。
使用我机器上添加的右括号,您的代码可以正常工作。 以下是一些可能有助于您从我的角度改进的建议:
在程序开始时加载数学模块 - 但是,如果你可以保持这种方式,并且在某些条件下会很好(比如你是否担心加载数学模块时的性能影响)不需要)。更多相关信息,如果您只想要平方根,则可以避免使用数学模块:((a**2)+(b**2))**0.5
应该可以做到这一点。
非常重要:检查用户输入。你不能使负整数的平方根。所以你最好做
c = int(input("what is the hypotenuse?(cm)"))
d = int(input("what is the otherside?(cm)"))
assert d < c, "The otherside cannot be greater than the hypothenuse"
import math
print("Other side=",math.sqrt((c**2)-(d**2)))
始终确保用户的输入。这是计划中最大的问题来源之一。菜单也是如此。 检查用户的输入是1,2或9.您可以设计一些内容以确保用户键入1,2或9,或者再次提示。
#With a printMenu function, you can print the menu again if you want to
def printMenu():
menu="Pythagoras Calculator:\n\
1. Find a hypotenuse?\n\
2. Find another side?\n\
9. Exit\n"
print(menu)
#By setting x to None initially, you allow the following while loop
# to be executed at least once.
x = None
previouslyBad=False # This will be a flag in order to know if user got it wrong the first time
printMenu()
#With a loop, user will be asked for input again and again unless
# he uses the solutions you wanted him to use.
while x not in [1, 2, 9]:
try:
if not previouslyBad:
x = int(input("Please choose a functionnality (e.g.: 1) "))
else:
x = int(input("Please enter either 1, 2 or 9 "))
#If you only use except without specifying ValueError,
# user won't be able to use Ctrl+C to exit your program and will be
# stuck in your menu. That is why you need to use two
# except statement.
except ValueError:
x = None
except:
raise
previouslyBad = True #If the loop loops more than once, it should be aware of it
if x == 1:
try:
a = int(input("what is the other side?(cm)"))
b = int(input("what is the last side?(cm)"))
#Would you like to ensure entered values were int and not raise
# error, you could use a similar strategy I implemented up with the
# menu.
except ValueError:
raise
print("Hypotenuse=",((a**2)+(b**2))**0.5) # Using math.sqrt is fine too. But lazy loading indicated you would focus on performance. This might be slightly faster since it doesn't need any module loading.
elif x == 2:
try:
c = int(input("what is the hypotenuse?(cm)"))
d = int(input("what is the otherside?(cm)"))
except ValueError:
raise
#Assert here allows you to ensure user won't try squarerooting a
# a negative value.
assert d <= c, "The otherside cannot be greater than the hypothenuse"
print("Other side=",((c**2)-(d**2))**0.5)
elif x == 9:
print("Good Bye")