[英]random.randint error no random on the code
AttributeError: 模塊 'random' 沒有屬性 'rendint'
import random
def guess(x):
random_number = random.rendint(1,x)
guess = 0
while guess != random_number:
guess = int(input(f"Guess a number between 1 and {x}"))
if guess < random_number:
print("Sorry, guess again. Too low.")
elif gues > random_number:
print("Sorry, guess again. Too high.")
print(f"Congrats! You have guessed the number{random_number}")
guess(10)
改變
random_number = random.rendint(1,x)
到
random_number = random.randint(1,x)
使用桑尼:
import random
def guess(x):
random_number = random.randint(1,x)
guess = 0
while guess != random_number:
guess = int(input(f"Guess a number between 1 and {x}\n"))
if guess < random_number:
print("Sorry, guess again. Too low.")
elif guess > random_number:
print("Sorry, guess again. Too high.")
print(f"Congrats! You have guessed the number {random_number}")
guess(10)
結果是:
>>> %Run guess.py
Guess a number between 1 and 10
5
Sorry, guess again. Too high.
Guess a number between 1 and 10
3
Sorry, guess again. Too low.
Guess a number between 1 and 10
4
Congrats! You have guessed the number 4
>>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.