如何让变量检测它是大于还是小于另一个? [英] How do I make a variable detect if it is greater than or less than another one?
问题描述
我目前正在学习 Python,我决定构建一个小型的猜数字"类型的游戏.我正在使用随机功能,并试图使其能够检测用户的输入是否等于随机数.这是我的代码:
I am currently learning Python, and I decided to build a small "Guess the Number" type of game. I am using the random feature, and trying to make it so it will detect if the user's input is equal the the random number. This is my code:
irand = randrange(1, 10)
while True:
number = input ("Pick a number 1-10: ")
if number < irand:
print (" ")
print ("That's too high, try again.")
if number > irand:
print (" ")
print ("That's too low, try again.")
if number == irand:
print (" ")
print ("You got it right! You won!")
break
我不确定如何让它检测 number
是否等于、小于或大于 irand
.有什么帮助吗?谢谢!:)
I am not exactly sure how to make it detect if number
is equal to, less than or greater than irand
. Any help? Thank you! :)
推荐答案
您检查等于、小于或大于"的代码是正确的,但是您更早遇到了问题在代码中.
Your code to check "equal to, less than, or greater than" is correct, but you've got a problem earlier in the code.
在 Python(3.0 及更高版本)中,input
返回一个字符串.因此,您将字符串 number
与数字 irand
进行比较.某些语言可能会自动转换一种或另一种方式,但这可能会令人困惑,*,因此 Python 拒绝这样做,而是引发 TypeError: unorderable types: int() <;str()
.
In Python (3.0 and later), input
returns a string. So, you're comparing number
, a string, to irand
, a number. Some languages might convert one way or the other automatically, but that can be confusing,*, so Python refuses to do so, and instead raises a TypeError: unorderable types: int() < str()
.
解决办法是把输入解析成一个数字,像这样:
The solution is to parse the input into a number, like this:
number = int(number)
如果用户给你无效输入,这将引发一个 ValueError
,比如字母 c
而不是 1 到 10 的数字.如果你想处理那个更好的是,您使用 try
语句.
This will raise a ValueError
if the user gives you invalid input, like the letter c
instead of a number from 1 to 10. If you want to deal with that more nicely, you use a try
statement.
作为旁注,您可能需要 elif
而不是 if
.如果你做对了一切,这没有任何区别,但如果你犯了错误,它有助于发现错误.
As a side note, you probably want elif
instead of if
. If you've got everything right, this doesn't make any difference, but if you've made a mistake, it helps catch the mistake.
所以,把它们放在一起:
So, putting it all together:
while True:
number = input("Pick a number 1-10: ")
try:
number = int(number)
except ValueError:
print(number, 'is not a number, try again.')
continue
if number < irand:
print(" ")
print("That's too high, try again.")
elif number > irand:
print(" ")
print("That's too low, try again.")
else:
print(" ")
print("You got it right! You won!")
break
(注意我在 except
子句中使用了 continue
,所以我们跳过循环的其余部分,不必担心 number
不再是一个数字.我们也可以将整个循环移动到 try
上的 else
原因中,或者添加一个 isinstance
检查在每个 if
等上,但这有点笨拙.)
(Notice that I used continue
in the except
clause, so we skip over the rest of the loop and don't have to worry about number
not being a number anymore. We could also move the whole loop into an else
cause on the try
, or add an isinstance
check on each if
, etc., but that gets a bit clumsy.)
* 考虑将字符串 "2"
与数字 10
进行比较.将字符串转换为数字的语言会说 2 <10
;将数字转换为字符串的语言会说 "10" <2"
.没有一个是对"或错",但两者都有一半的时间令人惊讶......
* Consider comparing the string "2"
to the number 10
. A language that converts the string to a number will say that 2 < 10
; a language that converts the number to a string will say that "10" < "2"
. Neither one is "right" or "wrong", but both are surprising half the time…
这篇关于如何让变量检测它是大于还是小于另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!