开发者

Guessing a number, true or false? Python

开发者 https://www.devze.com 2023-02-21 20:43 出处:网络
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...:

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('G开发者_如何学编程uess 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!


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!


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.


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.


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!


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.')


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号