2

So I'm new to this programming thing... But this has me stumped. To the point that I'm wondering if the website I'm running Python on is wrong. (repl.it is the website).

So I did one of those guess the number games as a small fun challenge. This is the code that I came up with:

from random import randint print ("Welcome to guess the number!") answer = str(randint(0,100)) print (answer) print () def something(): answerTwo = str(randint(0,100)) print (answerTwo) idea(answerTwo) def idea(x): number = str(input("Guess a number between 0 and 100:")) if number != x: if (number > x): print() print(number + " is too high!") print() idea(x) elif (number < x): print() print(number + " is too low!") print() idea(x) else: print() print ("That is correct!") again = input("Would you like to play again?:") if again == "yes": something() else: print ("Thanks for playing!") idea(answer) 

On the 4th and 8th line I print out the random number chosen so that I can quickly test to make sure everything works. Then I removed the print functions in the final product and tested again. Except when I removed the print functions it stopped working after some amount of time. For example, it'll say 39 is too low but 40 is too high, which is impossible since they're is no number in between them. If you put the print functions back in it works again, but remove them and it'll start acting up eventually.

I apologize if it's something really obvious but I just don't understand how this is possible.

Here is the github thingy for it

https://gist.github.com/anonymous/4a370664ae8ddb29aec5915eb20e686f

Thanks for your time!

5
  • If number is a str it won't be doing a numerical comparison. Commented May 11, 2017 at 22:32
  • So if I make number and answer not a string, will it work perfectly fine? I still don't understand what printing does to it though :/ Commented May 11, 2017 at 22:34
  • 1
    I really suggest you don't use recursion in this case... it's going to make your code difficult to debug. Commented May 11, 2017 at 23:17
  • @IanNelson, remove the casts to str() (since randint returns integers and you can still do the numeric comparison with the user inputs), which will also change the print() statements a bit (use string formatting to print the numbers, since print() won't concatenate strings and integers. Here's your code modified so it should behave the way you are expecting: repl.it/HvaN/0 , hope it helps. Commented May 12, 2017 at 18:01
  • Thank you so so much! That is incredibly helpful! Commented May 15, 2017 at 16:24

1 Answer 1

3

There is no integer i such that 39 < i < 40.

There is however a numeric string s such that "39" < s < "40". Observe:

>>> "39" < "4" < "40" True 

In short: It has nothing to do with your print calls, instead, just work on actual numbers and cast your input to a number using int(). print() can handle numbers just fine.

Sign up to request clarification or add additional context in comments.

1 Comment

So what would you suggest I change? Sorry, I'm really new

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.