我正在尝试学习如何编程,但遇到了问题....
我试图弄清楚如何确保有人输入数字而不是字符串。我发现的一些相关答案令人困惑,有些代码对我不起作用。我想有人发布了 try: 函数,但它不起作用,所以也许我需要导入一个库?
这是我现在正在尝试的:
代码:
print "Hi there! Please enter a number :)"
numb = raw_input("> ")
if numb != str()
not_a_string = int(next)
else:
print "i said a number, not a string!!!"
if not_a_string > 1000:
print "You typed in a large number!"
else:
print "You typed in a smaller number!"
我在问的时候还有另一个问题。我怎样才能让它接受大写和小写拼写?在下面的代码中,如果我输入“Go to the mall”但使用小写 G,它不会运行 if 语句,因为它只接受大写 G。
print "What would you like to do: \n Go to the mall \n Get lunch \n Go to sleep"
answer = raw_input("> ")
if answer == "Go to the mall":
print "Awesome! Let's go!"
elif answer == "Get lunch":
print "Great, let's eat!"
elif answer == "Go to sleep":
print "Time to nap!"
else:
print "Not what I had in mind...."
谢谢。^^
编辑:我也在使用 python 2.7 而不是 3.0