1
有一些小程序要求输入为char-y,b,s,然后通过比较它来检查输入是什么。
的概率”,当我运行程序,如果输入的是IS-字符串比较 - 未定义值
NameError: name 'b' is not defined
我得到,让说,B。
这是我的代码:
print("Please think of a number between 1 and 7")
print("Is it 4? (y,s,b)")
answer=char(input())
if (answer=='y'):
print("It is Four!")
elif (answer=='s'):
print("Is it 2? (y,s,b)")
answer=input()
if (answer=='y'):
print("It is two!")
elif (answer=='s'):
print("It is one!")
elif (answer=='b'):
print("It is Three!")
elif (answer=='b'):
print("Is it 6? (y,s,b)")
answer=input()
if (answer=='y'):
print("It is Six!")
elif (answer=='s'):
print("It is Five!")
elif (answer=='b'):
print("It is Seven!")
什么是y b和s? – IcyFlame 2013-03-12 09:14:07
是的,越来越小? – Ledhund 2013-03-12 09:15:51
@ user2160077:使用'if'和'elif'时,请删除括号。 – Matthias 2013-03-12 10:23:39