+ 3
Can this lovely community look over my poor attempt at a guess the number game template? I'm at a loss atm.
18 Respuestas
+ 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...
+ 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.
+ 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?
+ 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.
+ 2
Also yes num_guesses is the definition idk why I put guesses if it's not defined I'll change it
+ 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.
+ 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.
+ 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.
+ 2
Oh you're telling me to replace the pars with the function you showed me.
+ 2
Yeah, that should be enough functions for you to do what you need 🙂
+ 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?
+ 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"
+ 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.
+ 1
Russ, which line are you referring to? Line 10 or 24?
+ 1
Not implemented correctly? How so?
+ 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
+ 1
In line 45 and 46 the guess_clean is supposed to do that I just dont know how to implement it correctly.
+ 1
I see, so then what an I supposed to put in the pars_int function? Just the user input string?