简体   繁体   中英

Basic Rock, Paper, Scissors Doesn’t Work for my Calculator

I just started python 3, and it's my first language. This is my first attempt at anything, and I came up with the if statements and random.randint without looking up how to make rock paper and scissors, so the whole system is very flawed. That being said, it runs beautifully on my phone, how doesn't work for my calculator. It says something about how there is no attribute for randint, and I checked forums and they said that I must have a file already named random, but I don't anywhere on the device. What do I do? For some reason the app I use won't let me copy the code, but the best I could do was a link, the code is: https://codeplayground.app/?s=cPTCDE5g4U4DBoL7UfAFkc And the error message is: AttributeError: 'module' object has no attribute 'randint'

Edit- Sorry about improper etiquette, I do not know how to ask questions here this is my first one. Also, just to clarify, no files are named random, and this particular file was name “RPS” standing for Rock Paper Scissors

The code works for me on Colab. I assume you use a different code, where you actually use input() to capture "p". I suggest you try

p = int(input('Let me know your number')))

I'm assuming your file name when you run and get the attribute error is random.py. If that's the case, the problem is that you are having name collision with Python's "random" module. If I copy your code into a file called random.py, I get the attribute error. If I call the file something else, like "rock_paper_scissors.py" then it runs just fine. The problem is that Python is trying to resolve randint from your own random.py file which doesn't define randint! So you have to avoid naming your files the same as the modules you want to import.

You can also test this by putting something like this at the top of your file before your call to randint:

def randint(x, y):
    return 1

When you then call randint with this defined, you don't get the attribute error!

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