Python Forum
Fixing a code error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Fixing a code error (/thread-26746.html)



Fixing a code error - Twoshawns - May-12-2020

I am currently trying to practice python So I tried making a Rock paper scissors game where the player is against a computer but I don't understand how to fix my error in the program, can anyone help?
 
import random
import time
def main():
    print("This is a rock, paper, scissor game")
    
    while answer.upper == 'Y':
        return True
    return False
play_again = True
move_list = {'Rock', 'Paper', 'Scissors'}

def rps():
    print("This is a rock, paper, scissor game")
    time.sleep(3)
    move_list = ['Rock', 'Paper', 'Scissors']
    while play_again == True:
        for comp in move_list.keys():
            user = input('What\'s your hand?: ')
            user = user.capitalize()
            comp = random.choice(list(move_list.keys()))
            if comp == 'Rock' and user == 'Paper':
                print("Computer throws " + comp + " you have thrown " + user + "!")
                print("You Win!")

            elif comp == 'Rock' and user == 'Rock':
                print("Computer throws " + comp + " you have thrown " + user + "!")
                print("It's a draw")
            
            elif comp == 'Rock' and user == 'Scissor':
                print("Computer throws " + comp + " you have thrown " + user + "!")
                print("You Lose!")

            elif comp == 'Scissor' and user == 'Paper':
                print("Computer throws " + comp + " you have thrown " + user + "!")
                print("You Lose!")

            elif comp == 'Scissor' and user == 'Rock':
                print("Computer throws " + comp + " you have thrown " + user + "!")
                print("You Lose!")

            elif comp == 'Scissor' and user == 'Scissor':
                print("Computer throws " + comp + " you have thrown " + user + "!")
                print("It's a Draw!")

            elif comp == 'Paper' and user == 'Paper':
                print("Computer throws " + comp + " you have thrown " + user + "!")
                print("It's a Draw!")

            elif comp == 'Paper' and user == 'Rock':
                print("Computer throws " + comp + " you have thrown " + user + "!")
                print("You Lose!")

            elif comp == 'Paper' and user == 'Scissor':
                print("Computer throws " + comp + " you have thrown " + user + "!")
                print("You Win!")
            else:
                print("You messed up the program dummy")

        
    answer = input("Would you like to play again?: ")


rps()
main()
Error:
Traceback (most recent call last): File "F:\Programing\RPS Game Practice.py", line 62, in <module> rps() File "F:\Programing\RPS Game Practice.py", line 17, in rps for comp in move_list.keys(): AttributeError: 'list' object has no attribute 'keys'



RE: Fixing a code error - ndc85430 - May-12-2020

Why do you think a list has keys? Lists allow you to access their elements by index; dictionaries use keys.


RE: Fixing a code error - anbu23 - May-12-2020

Dictionary has keys. Check https://www.w3schools.com/python/python_dictionaries.asp

def rps():
    print("This is a rock, paper, scissor game")
    time.sleep(3)
    move_list = ['Rock', 'Paper', 'Scissors']
    while play_again == True:
        for comp in move_list:
            user = input('What\'s your hand?: ')
            user = user.capitalize()
            comp = random.choice(move_list)



RE: Fixing a code error - Twoshawns - May-14-2020

Thanks for the help, still learning python, but I ran into another problem with my while loop, not an error but when I prompt the user to type yes or no to playing again but no matter what it will continue to run the program.


RE: Fixing a code error - anbu23 - May-14-2020

Can you post code you tried?


RE: Fixing a code error - Twoshawns - May-14-2020

yeah sorry
[python]
import random
import time
def main():
print("This is a rock, paper, scissor game")

while play_again.upper == 'Y':
return True
return False
play_again = True


def rps():
print("This is a rock, paper, scissor game")
time.sleep(3)
move_list = ['Rock', 'Paper', 'Scissors']
while play_again == True:
for comp in move_list:
user = input('What\'s your hand?: ')
user = user.capitalize()
comp = random.choice(list(move_list))
if comp == 'Rock' and user == 'Paper':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Win!")

elif comp == 'Rock' and user == 'Rock':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("It's a draw")

elif comp == 'Rock' and user == 'Scissor':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Lose!")

elif comp == 'Scissors' and user == 'Paper':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Lose!")

elif comp == 'Scissors' and user == 'Rock':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Lose!")

elif comp == 'Scissors' and user == 'Scissors':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("It's a Draw!")

elif comp == 'Paper' and user == 'Paper':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("It's a Draw!")

elif comp == 'Paper' and user == 'Rock':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Lose!")

elif comp == 'Paper' and user == 'Scissors':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Win!")
else:
print("You messed up the program dummy")
print(comp)


answer = input("Would you like to play again?: ")

rps()
main()