+ 3
Can anyone help me fix the code , please !
I've created this first python project , but I got a error (even though I did the same code in visual studio code , it was working perfectly there , but not here) https://sololearn.com/compiler-playground/c79xgL8hDiwG/?ref=app
24 Antworten
+ 4
Here, in sololearn you have to provide all the inputs first like when you run the code it asks for inputs you have to fill all the inputs in the cell like:
1
2
3
..... And so on
+ 3
Thanks everyone for helping 😃
+ 2
It seems like the issue might be related to the condition in the while loop. It's set to `while count <= 10`, which means it will run until `count` becomes greater than 10, giving you an extra guess. To fix this, you can change it to `while count < 10` so that it runs until the 10th guess.
+ 2
Try this
import random
def guess_the_number():
num = random.randint(0,20)
count = 0
print("You have to guess a number within 10 guesses")
while count < 10: # Changed condition to count < 10
guess = int(input("Enter your guess: "))
if guess > num:
print("Try again, the guess is too big")
elif guess < num:
print("Try again, the guess is too small")
elif guess == num:
print("Congratulations! You found the number.")
print("You found the number in", count + 1, "guesses") # Added 1 to count since indexing starts from 0
break
count += 1
else:
print("Sorry, you've run out of guesses. The number was:", num)
guess_the_number()
+ 1
It's not opening
+ 1
Mahi This code playground requires all inputs upfront, which might cause issues with your code since it needs guesses and the number of inputs can vary.
+ 1
Mahi If you add a break to line 22 it will stop the code from running as soon as it finds the number.
Currently your code requires 11 inputs
2
4
9
11
13
15
16
17
18
19
20
Submit
https://sololearn.com/compiler-playground/c1MrcBeeIcaG/?ref=app
0
its true, it runs in VSCodium, but the 3rd guess is not equal with 2 guesses
0
Sorry , but I didn't get you
0
Ohh , means I have to create many variables for many inputs
0
Mahi I don't think just type the all inputs in the input box asked when going to run.
0
I'm still not getting it 😅
0
Actually , all the error comes is , the input , it asks one time - it doesn't matches , and now it should ask again but it didn't ask and gives a error
0
I did a little change but still error is at the input , I think I have to change the code itself
0
0
I have found the problem, the problem is not with your code, the problem is with how sololearn asks for user input(that's why it worked with visual studio and will work with python if you install it). On this app your guess game is pointless, because it asks for user input only once, so you can win it literaly by just typing 1-20 in every row. You can try running yr code here https://www.online-python.com/ .
0
Mahi
Actually you are dealing with two types of Errors in your code for two types of inputs. First is when your input is e.g. 1 3 5, it shows ValueError: invalid literal for in() with base 10: '1 3 5'.
Lets deal with it first, it occurs because the program expects a single integer input for each guess, but when you input multiple numbers separated by spaces, it tries to convert the entire string (including spaces) to an integer, resulting in a 'ValueError'. To resolve this issue, you can modify the program to handle multiple guesses entered in a single line, splitting the input string into individual guesses and processing them one by one.
[ Next ⤵️ ]
0
Mahi
Second is when your input is
e.g.
1
3
5, it show EOFError: EOF when reading a line. The EOFError likely occurs because the program expects the user to input guesses within the while loop, but the input stream is closed unexpectedly, causing the program to encounter the end of the file EOF. To resolve this error and prevent the program from crashing, you can catch the EOFError and handle it gracefully by displaying an appropriate message.
Our Sololearn, executes the code & captures any runtime errors that occur during execution. This includes errors like ValueError or EOFError that happen when the program runs into unexpected input or reaches the end of a file prematurely. This is definitely for educational purpose to teach how to handle errors professionally. To resolve these errors remember those tricks, I already debugged a lot of codes with those issues. Here is your code after resolving those errors. Enjoy and Happy Coding!
https://sololearn.com/compiler-playground/c6pVUrhMF7K5/?ref=app
0
Suhani it's showing same error. If wanna free your compiler out of Errors, you need to use my tricks here!
0
Hey any one can help me to create game codes