简体   繁体   中英

Python - Return problem with random.randint()

import random
    
print("""
___  ___          _           ___  ____           _ 
|  \/  |         | |          |  \/  (_)         | |
| .  . | __ _ ___| |_ ___ _ __| .  . |_ _ __   __| |
| |\/| |/ _` / __| __/ _ \ '__| |\/| | | '_ \ / _` |
| |  | | (_| \__ \ ||  __/ |  | |  | | | | | | (_| |
\_|  |_/\__,_|___/\__\___|_|  \_|  |_/_|_| |_|\__,_|

                                                    """)

meno = input("""
    Ahoj nový hráč. 
    Pre pokračovanie zadaj svoje meno: """)

print("""
    Ahoj {}
    Pravidlá sú následovné. ja si myslím číslo a ty budeš hádať.
    Ak chceš ukončiť hru, napíš 'KONIEC'.
    Na konci hry uvidíš svoje skóre.""".format(meno))

print("\nMyslím si číslo")

random = random.randint(1,11)
guess = -1
good = 0
bad = 0
alltry = 0
while True:
    guess = input("Tvoj typ: ").strip().lower()
    alltry += 1
    if guess == "koniec":
        alltry -= 1
        print("\n+{:=^30}+".format("KONIEC"))
        print("|{:^15}|{:^14}|".format("Správne", good))
        print("|{:^15}|{:^14}|".format("Nesprávne", bad))
        print("|{:^15}|{:^14}|".format("Spolu", alltry))
        print("+{:=^30}+".format(""))
        print("\nĎakujem za hru {}\n".format(meno))
        break
    if guess == "":
        print("NEZADAL SI CISLO!")
    elif int(guess) == random:
        good += 1
        print("Máš to!!!")
        random = random.randint(1,11)
        guess = -1
    elif int(guess) < 1 or int(guess) > 10:
        print("ZADÁVAJ ČÍSLA IBA Z INTERVALU OD 1 PO 10!")
    elif guess != random:
        print("NESPRÁVNE!\nHádaj znovu.")
        guess = -1
        bad += 1

Hi I have problem with this code. It is game MasterMind. If guess number, program return problem.

Traceback (most recent call last): File "mastermind.py", line 47, in <module> random = random.randint(1,11). AttributeError: 'int' object has no attribute 'randint'

Thanks for all help.

random = random.randint(1,11)

This statement rebinds the random name, which currently refers to the imported module (it was set when you did import random ), to an integer returned from randint() . The next time you execute this statement, random will no longer be the module, it will be an integer.

That's why it's complaining about trying to access a non-existent attribute of an int object.

The following transcript shows what's happening:

>>> import random
>>> type(random)
<class 'module'>

>>> random = random.randint(1,7)
>>> type(random)
<class 'int'>

>>> random = random.randint(1,7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'randint'

To fix, simply use a different name for the random value, such as randNum , so that random continues to be bound to the module.

You can not use random as a variable name since that is a module you are using, simply change the variable name and it should fix the issue.

Here I changed it to random_int instead and it works just fine.

import random

print("""
___  ___          _           ___  ____           _
|  \/  |         | |          |  \/  (_)         | |
| .  . | __ _ ___| |_ ___ _ __| .  . |_ _ __   __| |
| |\/| |/ _` / __| __/ _ \ '__| |\/| | | '_ \ / _` |
| |  | | (_| \__ \ ||  __/ |  | |  | | | | | | (_| |
\_|  |_/\__,_|___/\__\___|_|  \_|  |_/_|_| |_|\__,_|

                                                    """)

meno = input("""
    Ahoj nový hráč.
    Pre pokračovanie zadaj svoje meno: """)

print("""
    Ahoj {}
    Pravidlá sú následovné. ja si myslím číslo a ty budeš hádať.
    Ak chceš ukončiť hru, napíš 'KONIEC'.
    Na konci hry uvidíš svoje skóre.""".format(meno))

print("\nMyslím si číslo")

random_int = random.randint(1,11)
guess = -1
good = 0
bad = 0
alltry = 0
while True:
    guess = input("Tvoj typ: ").strip().lower()
    alltry += 1
    if guess == "koniec":
        alltry -= 1
        print("\n+{:=^30}+".format("KONIEC"))
        print("|{:^15}|{:^14}|".format("Správne", good))
        print("|{:^15}|{:^14}|".format("Nesprávne", bad))
        print("|{:^15}|{:^14}|".format("Spolu", alltry))
        print("+{:=^30}+".format(""))
        print("\nĎakujem za hru {}\n".format(meno))
        break
    if guess == "":
        print("NEZADAL SI CISLO!")
    elif int(guess) == random_int:
        good += 1
        print("Máš to!!!")
        random_int = random.randint(1,11)
        guess = -1
    elif int(guess) < 1 or int(guess) > 10:
        print("ZADÁVAJ ČÍSLA IBA Z INTERVALU OD 1 PO 10!")
    elif guess != random_int:
        print("NESPRÁVNE!\nHádaj znovu.")
        guess = -1
        bad += 1

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM