+ 3

Can this lovely community look over my poor attempt at a guess the number game template? I'm at a loss atm.

https://code.sololearn.com/clj3rf28YSo9/?ref=app

7th Jun 2020, 8:52 PM
John Perez
John Perez - avatar
18 Answers
+ 5
Line 13: player_input = str(int) It should be the other way around: int(str) Line 36: this loop won't run, because you declare guesses at 7, which is of course not greater than 7 Wait... you don't need the pars_int method at all, right? You are not using it in this code...
7th Jun 2020, 8:58 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
My advice would be to change your while loop to while guesses > 0: As it is, your loop will keep on goinf through 0 and into the negatives. With regards to the input issue, I would put that into its own function. def take_input(): while True: inp = input() try: inp = int(inp) return inp except: print("Incorrect input") This will keep looping as long as a number is not entered. You can adapt it to check that the number is in the correct range too.
7th Jun 2020, 9:24 PM
Russ
Russ - avatar
+ 3
If you are referring to the input function, you have kind of done that in your function (line 10) but not implemented it quite correctly. You will need to return the input so it is visible inside your main() function. Also, am I misreading or on line 40, you've decremented the variable "guesses", but the variable "num_guesses" is the one that the loop is depending on?
7th Jun 2020, 9:44 PM
Russ
Russ - avatar
+ 3
John Perez def take_input(): while True: inp = input() try: inp = int(inp) if inp < 0 or inp >= 101: continue return inp except: print("Incorrect input") Inside main() function: while num_guesses > 0: # need to change "<" to ">" here. print("Take a guess!") guess = take_input() This ensures that the code in main() only continues to run when the take_input() function has verified the input and returned the value to guess.
7th Jun 2020, 10:23 PM
Russ
Russ - avatar
+ 2
Also yes num_guesses is the definition idk why I put guesses if it's not defined I'll change it
7th Jun 2020, 9:55 PM
John Perez
John Perez - avatar
+ 2
By not implemented correctly, I mean it doesn't return anything and, if an incorrect input is entered, it doesn't loop around and ask the user to try again.
7th Jun 2020, 9:58 PM
Russ
Russ - avatar
+ 2
In fact, I've just realised that the try and except blocks are not even a part of the parse_int function so maybe I've completely misunderstood what the intention of that function was.
7th Jun 2020, 9:59 PM
Russ
Russ - avatar
+ 2
I'm trying to ensure that when the user inputs a number to guess the console checks the input to see if it is a valid entry. If not it needs to return None, other wise it continues until the user enters a valid integer but also if the user enters a value outside the range of the numbers it returns None ad well.
7th Jun 2020, 10:15 PM
John Perez
John Perez - avatar
+ 2
Oh you're telling me to replace the pars with the function you showed me.
7th Jun 2020, 10:58 PM
John Perez
John Perez - avatar
+ 2
Yeah, that should be enough functions for you to do what you need 🙂
8th Jun 2020, 6:56 AM
Russ
Russ - avatar
+ 1
Okay I fixed that but I'm concerned about the decrementing the guesses from 7 to 0 while also not decrementing them if the user input puts in an invalid input. I believe that's covered on the try exception at the top. I've seen alot of other examples of this code and they all increment the guesses not decrement. Is line 40 the correct way to do that?
7th Jun 2020, 9:07 PM
John Perez
John Perez - avatar
+ 1
You could actually envelope the whole while loop in the try/except clause (starting in line 37), so that you be sure it won't get executed until the input is of a valid form. Within the except block you could then write something like "Please provide a valid input"
7th Jun 2020, 9:18 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
Oh okay, yeah I didnt see that I was gonna call back to it from the main args part but that makes sense too. I'll do that.
7th Jun 2020, 9:25 PM
John Perez
John Perez - avatar
+ 1
Russ, which line are you referring to? Line 10 or 24?
7th Jun 2020, 9:35 PM
John Perez
John Perez - avatar
+ 1
Not implemented correctly? How so?
7th Jun 2020, 9:51 PM
John Perez
John Perez - avatar
+ 1
Oh okay I see now so the try except block should go in the main and I need to return the argument from the main to the pars got it
7th Jun 2020, 10:02 PM
John Perez
John Perez - avatar
+ 1
In line 45 and 46 the guess_clean is supposed to do that I just dont know how to implement it correctly.
7th Jun 2020, 10:21 PM
John Perez
John Perez - avatar
+ 1
I see, so then what an I supposed to put in the pars_int function? Just the user input string?
7th Jun 2020, 10:36 PM
John Perez
John Perez - avatar