0
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.

3
  • This is pretty simply: The line random = random.randint(1,11) turns random into a variable integer as opposed to a reference to the module called random. Rename your variable to random_int = random.randint(1,11) or something. Commented Aug 5, 2020 at 7:10
  • @niko, it's usually always simple to those answering the question, not necessarily so for those asking it :-) Commented Aug 5, 2020 at 7:18
  • 1
    @paxdiablo You are right (that being said it was not meant with a negative connotation in any form or shape) Commented Aug 5, 2020 at 7:26

2 Answers 2

3
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

@RadoslavDugas Mark the question as solved by pressing the check mark next to the correct answer. So that people know that the issule has been solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.