猜一个数字,真还是假? Python

发布于 2024-10-28 15:45:30 字数 298 浏览 2 评论 0原文

我想为某些东西分配一个值,然后尝试让某人猜测该值。我已经尝试过类似的方法,但我似乎无法让它发挥作用......:

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

放手` 2024-11-04 15:45:30

对于 python 版本 3

input() 返回一个“str”(字符串)对象。字符串与整数比较返回 False :

1 == '1'
False

您必须像这样转换输入:

guessfoo = int(input('Guess my number: '))

不要忘记尝试...除非输入的结果无法转换为 int 。

完整示例代码:

try:
    foo = 1
    guessfoo = int(input('Guess my number: '))
    if foo == guessfoo:
        print('Well done, You guessed it!')
    else:
        print('haha, fail.')
except ValueError:
    # cannot cast your input
    pass

编辑:

使用 python 版本 2

感谢您的评论:

在以前的版本中,输入将评估字符串,因此如果用户输入 1,输入将返回一个 int。

输出或您的原始代码:

$ python2 test.py 
Guess my number: 1
Well done, You guessed it!

With python version 3 :

input() returns a 'str' (string) object. A string compares to an integer returns False :

1 == '1'
False

You must cast your input like that :

guessfoo = int(input('Guess my number: '))

Don't forget to try...except if the result of the input cannot be casted into an int.

Full example code :

try:
    foo = 1
    guessfoo = int(input('Guess my number: '))
    if foo == guessfoo:
        print('Well done, You guessed it!')
    else:
        print('haha, fail.')
except ValueError:
    # cannot cast your input
    pass

EDIT:

With python version 2 :

Thanks for this comment :

In previous versions, input would eval the string, so if the user typed in 1, input returned an int.

Ouput or your original code:

$ python2 test.py 
Guess my number: 1
Well done, You guessed it!
指尖上的星空 2024-11-04 15:45:30

目前您没有条件停止循环。你只是在打印东西。猜出数字后放置一个 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 set loop to a different value than 1.

柠檬色的秋千 2024-11-04 15:45:30

因为整数 (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.

记忆之渊 2024-11-04 15:45:30

看起来第一个问题将是无限循环,因为您检查每次迭代的条件对于上述代码始终为真。当您循环时,您需要确保所做的更改将使循环更接近该条件。首先,您的代码应如下所示:

loop = 1
while loop == 1:
   foo = 1
   guessfoo = input('Guess my number: ')
   if foo == guessfoo:
       print('Well done, You guessed it!')
       loop = 0
   if foo != guessfoo:
       print('Oh no, try again')

如果实际猜到了数字,这将导致循环完成执行。

下一个问题是 input 返回一个字符串,因此检查输入的数字是否等于预期的数字总是会失败。在 python 中,使用 int() 函数将字符串转换为数字,因此该行如下所示:

guessfoo = int(input('Guess my number: '))

此时,您应该有一个正常工作的循环。但是,您可以采取一些措施来使代码更简单。这里有一些建议,从最简单的调整开始,然后转向更简洁的代码。

第一步可能是使用 if...else 来确保只执行一个条件,并且您只需检查 foo 的值一次。如果条件为真,则执行第一个分支;如果失败,则执行到 else 块。

loop = 1
while loop == 1:
   foo = 1
   guessfoo = int(input('Guess my number: '))
   if foo == guessfoo:
       print('Well done, You guessed it!')
       loop = 0
   else:
       print('Oh no, try again') 

这是可行的,但我们也可以将检查移至循环条件中以获取正确结果。这样,程序只会循环直到显示数字:

foo = 1
guessfoo = 0

while foo != guessfoo:
    guessfoo = int( input( 'Guess my number: ' )

    if guessfoo != foo:
        print( 'Oh no, try again' )

print( 'Well done, You guessed it!' )

现在只有当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:

loop = 1
while loop == 1:
   foo = 1
   guessfoo = input('Guess my number: ')
   if foo == guessfoo:
       print('Well done, You guessed it!')
       loop = 0
   if foo != guessfoo:
       print('Oh no, try again')

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 the int() function to convert a string to a number, so that line looks like:

guessfoo = int(input('Guess my number: '))

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 of foo once. If the condition is true, the first branch is executed; if it fails, execution proceeds to the else block.

loop = 1
while loop == 1:
   foo = 1
   guessfoo = int(input('Guess my number: '))
   if foo == guessfoo:
       print('Well done, You guessed it!')
       loop = 0
   else:
       print('Oh no, try again') 

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:

foo = 1
guessfoo = 0

while foo != guessfoo:
    guessfoo = int( input( 'Guess my number: ' )

    if guessfoo != foo:
        print( 'Oh no, try again' )

print( 'Well done, You guessed it!' )

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!

醉南桥 2024-11-04 15:45:30

另外,您应该使用两个不同的 if 吗?
难道不应该是更像

if foo == guessfoo:
   print('Well done, you guessed it!')
else:
   print('haha, fail.')

Also, should you be using two different if's?
shouldn't it be something more like

if foo == guessfoo:
   print('Well done, you guessed it!')
else:
   print('haha, fail.')
淡淡绿茶香 2024-11-04 15:45:30
import random

try:
    inp = raw_input   # Python 2.x
except NameError:
    inp = input       # Python 3.x

foo = random.randint(1,10)
while True:
    guess = int(inp('Guess my number: '))
    if guess < foo:
        print('Too low!')
    elif guess > foo:
        print('Too high!')
    else:
        print('You got it!')
        break
import random

try:
    inp = raw_input   # Python 2.x
except NameError:
    inp = input       # Python 3.x

foo = random.randint(1,10)
while True:
    guess = int(inp('Guess my number: '))
    if guess < foo:
        print('Too low!')
    elif guess > foo:
        print('Too high!')
    else:
        print('You got it!')
        break
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文