Reputation:
I am writing a program in python where you have to guess the number chosen by the computer. The number will be chosen between 0 and 10(inclusive of both the number). If the number guessed by the user is correct or wrong, a message will be shown based on the validity of the input (the number guessed by the user), followed by the number chosen by the computer as the outputs. The output should be something like this.
ENTER: 4
Wrong
10 (Computer's choice)
OR
ENTER: 10
RIGHT!
10 (Computer's Choice)
My Code:
from random import randint
def guessing_game(x):
guess = randint(0,10)
if x.isdigit():
if x==guess:
return "RIGHT!"
print(" COMPUTER =" guess)
else:
return "Wrong"
print("COMPUTER="guess)
elif x.isdigit() == False:
return " Enter a Number"
print(guessing_game(input("ENTER:")))
The Problem is that the computer's choice(Computer:10)
is not appearing on the console window.
The IDE on which I am making this program is telling me that "CODE IS UNREACHABLE".
THE ACTUAL OUTPUT:
ENTER:4
Wrong
Hence, please help me to solve this issue. Every suggestion will be appreciated.
THANK YOU
Upvotes: 0
Views: 36
Reputation: 51683
Your
guess = randint(0,10)
is of type int.
Your
print(guessing_game(input("ENTER:")))
is of type string.
This test
if x==guess:
can never be True
.
Beside that: return
jumps out, nothing after it happes.
Use:
def guessing_game(x):
guess = str(randint(0,10)) # make it a string
if x==guess: # compare two strings
print(" COMPUTER =" guess) # print first, then return. return jumps out!
return "RIGHT!"
else:
print("COMPUTER="guess)
return "Wrong"
If the user inputs something other then a numnber, their problem.
Upvotes: 0
Reputation: 4640
Your print is placed after a return
instruction. That means at the execution in the case the user as found the guessed number, your function will return "RIGHT!" and stop execution of further code within this function.
That's why pycharm indicate that the code is unreachable
Upvotes: 2