I want to code num_guess using def.
Existing num_guess coding is
import random
answer = random.randint(1,100)
try_cnt = 5
while True:
if try_cnt == 0:
print("Game over.\nThe Answer is {}".format(answer))
break
your_answer = eval("Please enter a number":"))
if answer == your_answer:
print("Right Answer!")
break
elif answer > your_answer:
try_cnt -= 1
print("Answer is over than your_answer.")
else:
try_cnt -= 1
print("Answer is less than your_answer.")
I don't know how to code by specifying parameters부 답변 Please answer me,,
python def numguess
I don't understand the question, but...I think they want to handle it as a function.
Please refer to the code below.
def num_guess(answer, try_cnt=5):
for _ in range(try_cnt):
your_answer = int(input("Please enter a number"):"))
if answer == your_answer:
print("Right Answer!")
return
print("Answer is over than your_answer." if answer > your_answer else "Answer is less than your_answer.")
print("Game over.\nThe Answer is {}".format(answer))
import numpy as np
a = np.random.randint(0, 100)
num_guess(a)
Please enter a number:40
Answer is over than your_answer.
Please enter a number: 50
Answer is over than your_answer.
Please enter a number: 60
Answer is less than your_answer.
Please enter a number:55
Answer is over than your_answer.
Please enter a number: 56
Answer is over than your_answer.
Game over.
The Answer is 57
865 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
566 Who developed the "avformat-59.dll" that comes with FFmpeg?
567 Understanding How to Configure Google API Key
592 GDB gets version error when attempting to debug with the Presense SDK (IDE)
593 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.