我相信这会有效,但它会在代码的最后一行给我一个错误信息,因为语法无效,我不确定为什么要寻求帮助。
password=123
ask=raw_input("What is the password")
try:
while ask=="123":
age=int(raw_input("What is your age"))
if age >= 18:
print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")
答案 0 :(得分:0)
以下是经过一些细微更改后的代码的工作版本:
password = "123"
ask = raw_input("What is the password?\n")
if ask == password:
age = int(raw_input("What is your age?\n"))
if age >= 18:
print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")
答案 1 :(得分:0)
问题是try
语句总是需要except
子句在语法上有效。
在您的情况下,try
语句没有意义(与程序的其他部分一样),因此您应该删除它并相应地突出while
块。
可以防止非整数输入,但如果这是你的意图,你应该把它放在那个位置:
password = "123"
ask = raw_input("What is the password")
if ask == password:
while True:
try:
age = int(raw_input("What is your age? "))
break
except ValueError:
print "Please enter an integer number!"
if age >= 18:
print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")