猜一个数字,真还是假? Python
我想为某些东西分配一个值,然后尝试让某人猜测该值。我已经尝试过类似的方法,但我似乎无法让它发挥作用......:
foo = 1
guessfoo = input('Guess my number: ')
if foo == guessfoo:
print('Well done, You guessed it!')
if foo != guessfoo:
print('haha, fail.')
为什么这不起作用?我应该怎么做呢?我是这方面的初学者,请帮忙!
I want to assign something a value and then try and get someone to guess that value. I have tried something along the lines of this but I can't seem to get it to work...:
foo = 1
guessfoo = input('Guess my number: ')
if foo == guessfoo:
print('Well done, You guessed it!')
if foo != guessfoo:
print('haha, fail.')
Why doesn't this work? And how should I be doing it? I am a beginner at this, please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于 python 版本 3 :
input() 返回一个“str”(字符串)对象。字符串与整数比较返回 False :
您必须像这样转换输入:
不要忘记尝试...除非输入的结果无法转换为 int 。
完整示例代码:
编辑:
使用 python 版本 2:
感谢您的评论:
输出或您的原始代码:
With python version 3 :
input() returns a 'str' (string) object. A string compares to an integer returns False :
You must cast your input like that :
Don't forget to try...except if the result of the input cannot be casted into an int.
Full example code :
EDIT:
With python version 2 :
Thanks for this comment :
Ouput or your original code:
目前您没有条件停止循环。你只是在打印东西。猜出数字后放置一个
break
,或者将loop
设置为不同于 1 的值。Currently you have no condition to stop the loop. You're just printing stuff. Put a
break
after you guessed the number, or setloop
to a different value than 1.因为整数 (foo) 不是字符串(input() 的结果)。
如果您认为 Python 的行为类似于 PHP 或 Perl,那么您就错了,
1 与“1”完全不同。
您必须使用 int(..) 将字符串转换为 int 或检查 'foo'
作为字符串。
Because integers (foo) aren't strings (result of input()).
If you think that Python behaves like PHP or Perl then you are wrong
1 is something completely different than '1'.
You have to either convert the string using int(..) to an int or check against 'foo'
as a string.
看起来第一个问题将是无限循环,因为您检查每次迭代的条件对于上述代码始终为真。当您循环时,您需要确保所做的更改将使循环更接近该条件。首先,您的代码应如下所示:
如果实际猜到了数字,这将导致循环完成执行。
下一个问题是
input
返回一个字符串,因此检查输入的数字是否等于预期的数字总是会失败。在 python 中,使用int()
函数将字符串转换为数字,因此该行如下所示:此时,您应该有一个正常工作的循环。但是,您可以采取一些措施来使代码更简单。这里有一些建议,从最简单的调整开始,然后转向更简洁的代码。
第一步可能是使用
if...else
来确保只执行一个条件,并且您只需检查foo
的值一次。如果条件为真,则执行第一个分支;如果失败,则执行到else
块。这是可行的,但我们也可以将检查移至循环条件中以获取正确结果。这样,程序只会循环直到显示数字:
现在只有当
foo ==guessfoo
时才会显示成功消息。这个循环更加清晰和简单。作为初学者,您选择了一个寻求帮助的好地方!欢迎来到 StackOverflow!
It looks like the first problem will be an infinite loop, since the condition you're checking each iteration will always be true with the above code. When you loop, you need to make sure you change something that will bring loop closer to that condition. As a start, your code should look like this:
This will cause the loop to finish executing if the number is actually guessed.
The next problem is that
input
returns a String, so the check to see if the entered number is equal to the expected number will always fail. In python, use theint()
function to convert a string to a number, so that line looks like:At this point, you should have a decently-working loop. However, there are a few things you could do to make your code simpler. Here are some suggestions, starting with the simplest tweaks and moving to cleaner code.
The first step could be to use
if...else
to make sure only one condition is executed, and you only have to check the value offoo
once. If the condition is true, the first branch is executed; if it fails, execution proceeds to theelse
block.This works, but we can also move the check for a correct result in the condition of the loop. This way, the program only loops until the number is displayed:
Now the success message will only be displayed when
foo == guessfoo
. This loop is a bit clearer and simpler.As a beginner, you've chosen a great place to ask for help! Welcome to StackOverflow!
另外,您应该使用两个不同的 if 吗?
难道不应该是更像
Also, should you be using two different if's?
shouldn't it be something more like