Python Forum

Full Version: Need help with fixing iteration counter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Probably a brainlet question, but I will ask anyway since I'm too lazy to actually read the documentation.
I've been trying to do this exercise: https://www.practicepython.org/exercise/...e-one.html

Basically : Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right.

Now this works proper, but I've decided to also add a counter that counts every guess the user takes and display it at the end...But I screwed up, and I have no idea why Wall , as this usually works in languages like C#.
The code:

"""17.10.18
guessing game one
generates a random number between 1 and 9 (including 1 and 9)
then asks the user to guess a number to see how close it is to the solution
"""

#importing modules
import random

#declaring variables
rnd = random.randint(1, 9)
num = 0
count = 1
#user input
while num != rnd:
    num = input("Guess a number between 1 & 9: ")

    if num != rnd and num < rnd:
        print("Too small, try again...")
        count++

    elif num != rnd and num > rnd:
        print("Too large, try again...") 
        count++

print("Good job! it took you " + count + " guesses to win.")
quit()
Any help is appreciated Dance !
(Oct-17-2018, 09:17 PM)Kapolt Wrote: [ -> ]I will ask anyway since I'm too lazy to actually read the documentation.

this statement is something that will definitely demotivate people to answer your question.
(Oct-17-2018, 09:22 PM)buran Wrote: [ -> ]
(Oct-17-2018, 09:17 PM)Kapolt Wrote: [ -> ]I will ask anyway since I'm too lazy to actually read the documentation.

this statement is something that will definitely demotivate people to answer your question.
Bummer, can you at least point me to the right page? I'm pretty lost as to where and what I should read in order to solve this issue...
There is no ++ in Python. Use += 1. Note that your count will be wrong. Also note that if one number is less than another number, those numbers are not equal.
In addition to everything ichabod said input() will return str and you are comparing it with int, so you will be in infinite loop.
(Oct-18-2018, 05:56 AM)buran Wrote: [ -> ]In addition to everything ichabod said input() will return str and you are comparing it with int, so you will be in infinite loop.

Wouldn't that work if OP was using 2.7?
(Oct-18-2018, 01:09 PM)ichabod801 Wrote: [ -> ]Wouldn't that work if OP was using 2.7?
I guessed it's python3, because of the print function, but of course you are right that it will work in python2.
(Oct-18-2018, 05:32 PM)buran Wrote: [ -> ]
(Oct-18-2018, 01:09 PM)ichabod801 Wrote: [ -> ]Wouldn't that work if OP was using 2.7?
I guessed it's python3, because of the print function, but of course you are right that it will work in python2.

Checked my sys.version, turns out I actually use python2...
While everything works, I'm a tiny bit confused...What's the difference in a nutshell between python3 and python2? and also, why wouldn't
the program run if I wrote in in python3 and how can I fix it?
Cheers
(Oct-18-2018, 07:46 PM)Kapolt Wrote: [ -> ]What's the difference in a nutshell between python3 and python2?

https://docs.python.org/3/whatsnew/3.0.html
https://wiki.python.org/moin/Python2orPython3

Quote:Short version: Python 2.x is legacy, Python 3.x is the present and future of the language

(Oct-18-2018, 07:46 PM)Kapolt Wrote: [ -> ]why wouldn't the program run if I wrote in in python3 and how can I fix it?

https://python-forum.io/Thread-Python3-2...-raw-input