I want to code to guess numbers using def.

Asked 2 years ago, Updated 2 years ago, 65 views

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

2022-09-22 08:04

1 Answers

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


2022-09-22 08:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.