How to solve 'Error: expect 2, got 1' if I can only Input one value?
Question:
I am a newbie in programming and I am doing my python assignment. Sorry if my problem is silly.
I was asked to Write a python program for a two-player rolling dice game.
Each player has a counter to record their scores. Each time, a player would roll two dice. The sum of the two dice would be added to the corresponding player’s counter. If the player rolls doubles, then the player can continue to roll the dice. Otherwise, it would be the other player’s turn. When the input is 0, print the players’ scores.
Example input and output:
INPUT:
12
31
23
0
OUTPUT:
P1:8 P2:4
ERROR message:
not enough values to unpack (expected 2, got 1)
*Since this assignment is beginner-level, some complicated languages and functions are not allowed. Only list, dictionary, loop, and other basic languages are allowed
p1score = 0
p2score = 0
while True:
p1roll1, p1roll2 = list(input())
if p1roll1 == 0:
print(f'P1:{plscore} P2:{p2score}')
break
elif p1roll1 == p1roll2:
p1score += int(p1roll1) + int(p1roll2)
continue
elif p1roll1 != p1roll2:
continue
p2roll1, p2roll2 = list(input())
if p2roll1 == 0:
print(f'P1:{p1score} P2:{p2score}')
break
elif p2roll1 == p2roll2:
p2score += int(p2roll1) + int(p2roll2)
continue
elif p2roll1 != p2roll2:
continue
This is my code now. However, since ‘0’ needed to be input to end the game, a single input cannot satisfy the requirement of input and an error pops out.
Is there any way to solve this? Or am I needed to rewrite the whole thing?
Answers:
Accept the input into a plain variable before you do any special processing on it.
while True:
answer = input()
if answer == "0":
break
p1roll1, p1roll2 = answer
I am a newbie in programming and I am doing my python assignment. Sorry if my problem is silly.
I was asked to Write a python program for a two-player rolling dice game.
Each player has a counter to record their scores. Each time, a player would roll two dice. The sum of the two dice would be added to the corresponding player’s counter. If the player rolls doubles, then the player can continue to roll the dice. Otherwise, it would be the other player’s turn. When the input is 0, print the players’ scores.
Example input and output:
INPUT:
12
31
23
0
OUTPUT:
P1:8 P2:4
ERROR message:
not enough values to unpack (expected 2, got 1)
*Since this assignment is beginner-level, some complicated languages and functions are not allowed. Only list, dictionary, loop, and other basic languages are allowed
p1score = 0
p2score = 0
while True:
p1roll1, p1roll2 = list(input())
if p1roll1 == 0:
print(f'P1:{plscore} P2:{p2score}')
break
elif p1roll1 == p1roll2:
p1score += int(p1roll1) + int(p1roll2)
continue
elif p1roll1 != p1roll2:
continue
p2roll1, p2roll2 = list(input())
if p2roll1 == 0:
print(f'P1:{p1score} P2:{p2score}')
break
elif p2roll1 == p2roll2:
p2score += int(p2roll1) + int(p2roll2)
continue
elif p2roll1 != p2roll2:
continue
This is my code now. However, since ‘0’ needed to be input to end the game, a single input cannot satisfy the requirement of input and an error pops out.
Is there any way to solve this? Or am I needed to rewrite the whole thing?
Accept the input into a plain variable before you do any special processing on it.
while True:
answer = input()
if answer == "0":
break
p1roll1, p1roll2 = answer