I am trying to create a simple Rock, Paper, Scissors game to practice some basic Python skills. I created the code below to take user input and create a simple point-based game. However, when I ran the code, the loop ran infinitely, printing the output message until I force stopped it. How can I alter my code so the while-loop only runs once per user input?
code:
JavaScript
x
82
82
1
import random as rand
2
3
print("Welcome to Rock, Paper, Scissors!")
4
5
x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")
6
7
game_choices = ("Rock",
8
"Paper",
9
"Scissors")
10
11
comp_input = rand.choice(game_choices)
12
13
phrase_one = "It was a tie!"
14
phrase_two = "You win! WOOOOOOO go Grandma!!!"
15
phrase_three = "You lost.. shoulda had a V8!"
16
17
my_score = 0
18
19
print("The computer chose: ",comp_input)
20
21
while my_score >= 0:
22
if x == "R" and comp_input == "Rock":
23
print(phrase_one)
24
print("Score: ",my_score)
25
if my_score < 0:
26
print("Game over :( Also, YOU SUCK!")
27
break
28
elif x == "S" and comp_input == "Scissors":
29
print(phrase_one)
30
print("Score: ",my_score)
31
if my_score < 0:
32
print("Game over :( Also, YOU SUCK!")
33
break
34
elif x == "P" and comp_input == "Paper":
35
print(phrase_one)
36
print("Score: ",my_score)
37
if my_score < 0:
38
print("Game over :( Also, YOU SUCK!")
39
break
40
elif x == "R" and comp_input == "Scissors":
41
print(phrase_two)
42
my_score += 1
43
print("Score: ",my_score)
44
if my_score < 0:
45
print("Game over :( Also, YOU SUCK!")
46
break
47
elif x == "R" and comp_input == "Paper":
48
print(phrase_three)
49
my_score -= 1
50
print("Score: ",my_score)
51
if my_score < 0:
52
print("Game over :( Also, YOU SUCK!")
53
break
54
elif x == "S" and comp_input == "Rock":
55
print(phrase_three)
56
my_score -= 1
57
print("Score: ",my_score)
58
if my_score < 0:
59
print("Game over :( Also, YOU SUCK!")
60
break
61
elif x == "S" and comp_input == "Paper":
62
print(phrase_two)
63
my_score += 1
64
print("Score: ",my_score)
65
if my_score < 0:
66
print("Game over :( Also, YOU SUCK!")
67
break
68
elif x == "P" and comp_input == "Rock":
69
print(phrase_two)
70
my_score += 1
71
print("Score: ",my_score)
72
if my_score < 0:
73
print("Game over :( Also, YOU SUCK!")
74
break
75
elif x == "P" and comp_input == "Scissors":
76
print(phrase_three)
77
my_score -= 1
78
print("Score: ",my_score)
79
if my_score < 0:
80
print("Game over :( Also, YOU SUCK!")
81
break
82
Advertisement
Answer
the problem is that unless you loose on your first round you keep winning for ever because none of the input values changed. all you have to do is move your input() and the computers random selection into the loop like this
JavaScript
1
84
84
1
import random as rand
2
3
print("Welcome to Rock, Paper, Scissors!")
4
game_choices = ("Rock",
5
"Paper",
6
"Scissors")
7
8
9
10
phrase_one = "It was a tie!"
11
phrase_two = "You win! WOOOOOOO go Grandma!!!"
12
phrase_three = "You lost.. shoulda had a V8!"
13
14
my_score = 0
15
while my_score >= 0:
16
17
x = input("nYour move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ").upper()
18
comp_input = rand.choice(game_choices)
19
20
print("The computer chose: ",comp_input)
21
22
23
if x == "R" and comp_input == "Rock":
24
print(phrase_one)
25
print("Score: ",my_score)
26
if my_score < 0:
27
print("Game over :( Also, YOU SUCK!")
28
break
29
elif x == "S" and comp_input == "Scissors":
30
print(phrase_one)
31
print("Score: ",my_score)
32
if my_score < 0:
33
print("Game over :( Also, YOU SUCK!")
34
break
35
elif x == "P" and comp_input == "Paper":
36
print(phrase_one)
37
print("Score: ",my_score)
38
if my_score < 0:
39
print("Game over :( Also, YOU SUCK!")
40
break
41
elif x == "R" and comp_input == "Scissors":
42
print(phrase_two)
43
my_score += 1
44
print("Score: ",my_score)
45
if my_score < 0:
46
print("Game over :( Also, YOU SUCK!")
47
break
48
elif x == "R" and comp_input == "Paper":
49
print(phrase_three)
50
my_score -= 1
51
print("Score: ",my_score)
52
if my_score < 0:
53
print("Game over :( Also, YOU SUCK!")
54
break
55
elif x == "S" and comp_input == "Rock":
56
print(phrase_three)
57
my_score -= 1
58
print("Score: ",my_score)
59
if my_score < 0:
60
print("Game over :( Also, YOU SUCK!")
61
break
62
elif x == "S" and comp_input == "Paper":
63
print(phrase_two)
64
my_score += 1
65
print("Score: ",my_score)
66
if my_score < 0:
67
print("Game over :( Also, YOU SUCK!")
68
break
69
elif x == "P" and comp_input == "Rock":
70
print(phrase_two)
71
my_score += 1
72
print("Score: ",my_score)
73
if my_score < 0:
74
print("Game over :( Also, YOU SUCK!")
75
break
76
elif x == "P" and comp_input == "Scissors":
77
print(phrase_three)
78
my_score -= 1
79
print("Score: ",my_score)
80
if my_score < 0:
81
print("Game over :( Also, YOU SUCK!")
82
break
83
84