Python Forum
Need help with fixing iteration counter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with fixing iteration counter
#1
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 !
Reply
#2
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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...
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
In addition to everything ichabod said input() will return str and you are comparing it with int, so you will be in infinite loop.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(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
Reply
#9
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  fixing error TypeError: 'float' object is not subscriptable programmingirl 1 1,477 Jan-28-2023, 08:13 PM
Last Post: deanhystad
  Need help fixing Index Error adesimone 1 1,259 Nov-21-2022, 07:03 PM
Last Post: deanhystad
  Fixing my backup function Beginner_coder123 4 2,963 Jun-15-2019, 07:56 PM
Last Post: Beginner_coder123
  Fixing arrays output in Python WLST script pkbash 2 3,945 Feb-28-2019, 06:20 PM
Last Post: pkbash
  i need help in fixing my scientific calculator coding : (, im using python 3.5.5 hans77 1 4,120 Oct-17-2018, 03:26 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020