Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fixing a code error
#1
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'
Reply
#2
Why do you think a list has keys? Lists allow you to access their elements by index; dictionaries use keys.
Reply
#3
Dictionary has keys. Check https://www.w3schools.com/python/python_...naries.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)
Reply
#4
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.
Reply
#5
Can you post code you tried?
Reply
#6
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid syntax error - need help fixing calgk01 3 830 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Help Fixing Code kianwalters05 5 1,122 May-12-2020, 12:13 PM
Last Post: kianwalters05
  Fixing "PermissionError: [Errno 13] Permission denied" puredata 17 37,056 Mar-09-2020, 03:20 PM
Last Post: syssy
  Fixing the code khanhcao 2 821 Feb-06-2020, 07:24 AM
Last Post: buran
  Fixing a problem with file.io ThickTac 2 1,329 Mar-13-2019, 10:13 PM
Last Post: ThickTac
  Fixing indentation issues. MuntyScruntfundle 9 1,866 Feb-02-2019, 05:55 PM
Last Post: snippsat
  Can anyone Please help on fixing this python code to connect and save things on sqlit Roscoes 2 1,679 Mar-06-2018, 04:48 AM
Last Post: Roscoes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020