Comparing user input to a randomly selected list item - Python -
i creating rock, paper, scissors game class. part of game need have weapon menu display screen user select from. computer randomly select weapon list. problem facing (i believe) list items range [0,2] menu items list [1,3]. have searched around hours, don't understand complex things have been reading online i'm not how apply them.
# random integer random import randint # list weapon weapon = ["rock", "paper", "scissors"] # 1 player mode def oneplayer(): scorep = 0 scorec = 0 again = "" player = false print("---------------------------------------------") print("\n\tplayer vs computer") while player == false: print("weapons:") print("1. rock") print("2. paper") print("3. scissors") print("4. quit") player = input("\nselect weapon: ") if player == "quit" or player == "q" or player == "4": player = true main() else: try: player = int(player) if player == 1: player = weapon[0] elif player == 2: player = weapon[1] elif player == 3: player = weapon[2] except: print("please enter number 1 through 4\n") computer = weapon[randint(0,2)] if player == computer: print(player," vs ",computer) print("it's tie!\n") print("player:",scorep,"\ncomputer:",scorec) print("") elif player == 1: # computer == paper if computer == 1: print(player," vs ",computer) print("paper covers rock! lose!\n") scorec = scorec + 1 print("player:",scorep,"\ncomputer:",scorec) print("") else: print("rock smashes scissors. win!\n") scorep = scorep + 1 print("player:",scorep,"\ncomputer:",scorec) print("") elif player == 2: if computer == 2: print(player," vs ",computer) print("scissors cut paper! lose!\n") scorec = scorec + 1 print("player:",scorep,"\ncomputer:",scorec) print("") else: print("paper covers rock. win!\n") scorep = scorep + 1 print("player:",scorep,"\ncomputer:",scorec) print("") elif player == 3: if computer == 0: print(player," vs ",computer) print("rock smashes scissors! lose!\n") scorec = scorec + 1 print("player:",scorep,"\ncomputer:",scorec) print("") else: print("scissors cut paper. win!\n") scorep = scorep + 1 print("player:",scorep,"\ncomputer:",scorec) print("") #else: # print("please select valid play option\n") player = false
please don't mind print statements inside if/else statements. realize these need changed. main issue logic of comparing user input computer's random list selection.
you need careful contents of variables:
# storing string computer = weapon[randint(0,2)] # expects integer elif player == 1: # computer == paper if computer == 1:
that root of of problems seeing.
also, in general, when coding try use meaningful variable names , avoid reusing them more 1 purpose: in case, 2 new variables player_weapon , computer_weapon (instead of reusing player , computer) have prevented bug. don't lazy when declaring variables! ;)
Comments
Post a Comment