AttributeError: module 'random' has no attribute '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)
Change
random_number = random.rendint(1,x)
to
random_number = random.randint(1,x)
Using Thonny:
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)
Results in:
>>> %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
>>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.