Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Need help with fixing iteration counter Kapolt Unladen Swallow Posts: 3 Threads: 1 Joined: Oct 2018 Reputation: 0 Likes received: 0 #1 Oct-17-2018, 09:17 PM 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 , 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 ! buran Posts: 4,454 Threads: 82 Joined: Sep 2016 Reputation: 279 Likes received: 970 #2 Oct-17-2018, 09:22 PM (This post was last modified: Oct-17-2018, 09:23 PM by buran. Edited 1 time in total.) (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. Kapolt Unladen Swallow Posts: 3 Threads: 1 Joined: Oct 2018 Reputation: 0 Likes received: 0 #3 Oct-17-2018, 09:24 PM (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... ichabod801 Bunny Rabbit Posts: 3,805 Threads: 82 Joined: Sep 2016 Reputation: 229 Likes received: 1155 #4 Oct-18-2018, 01:58 AM 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 buran Posts: 4,454 Threads: 82 Joined: Sep 2016 Reputation: 279 Likes received: 970 #5 Oct-18-2018, 05:56 AM In addition to everything ichabod said `input()` will return str and you are comparing it with int, so you will be in infinite loop. ichabod801 Bunny Rabbit Posts: 3,805 Threads: 82 Joined: Sep 2016 Reputation: 229 Likes received: 1155 #6 Oct-18-2018, 01:09 PM (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 buran Posts: 4,454 Threads: 82 Joined: Sep 2016 Reputation: 279 Likes received: 970 #7 Oct-18-2018, 05:32 PM (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. Kapolt Unladen Swallow Posts: 3 Threads: 1 Joined: Oct 2018 Reputation: 0 Likes received: 0 #8 Oct-18-2018, 07:46 PM (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 buran Posts: 4,454 Threads: 82 Joined: Sep 2016 Reputation: 279 Likes received: 970 #9 Oct-18-2018, 07:56 PM (This post was last modified: Oct-18-2018, 07:56 PM by buran. Edited 1 time in total.) (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 « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Fixing my backup function Beginner_coder123 4 358 Jun-15-2019, 07:56 PM Last Post: Beginner_coder123 Fixing arrays output in Python WLST script pkbash 2 412 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 791 Oct-17-2018, 03:26 AM Last Post: stullis

Forum Jump:

Users browsing this thread: 1 Guest(s)