我是编程新手,现在已经努力解决问题3天了。帮助将不胜感激!我建立了一个石头/纸/剪刀游戏,但除了1件事外,它都可以正常工作:当P2(玩家2)输入r / p / s之外的其他内容时,程序会告诉它输入无效,然后跳回P1 (播放器1)输入-我希望它在P2继续进行,而不是再次从P1开始。现在我知道有大约100种方法可以更好地编写程序,但是我想使用当前代码对其进行修复-我猜是在另一个while循环之类的东西,但是尝试了无数的选项和组合却没有成功。有人帮忙吗?
def rps():
if p1 == 'r' and p2 == 's':
print('Congrats P1! ')
elif p1 == 'r' and p2 == 'p':
print('Congrats P2! ')
elif p1 == 'p' and p2 == 's':
print('Congrats P2! ')
elif p1 == 'p' and p2 == 'r':
print('Congrats P1! ')
elif p1 == 's' and p2 == 'r':
print('Congrats P2! ')
elif p1 == 's' and p2 == 'p':
print('Congrats P1! ')
elif p1 == 'r' and p2 == 'r':
print('Draw! ')
elif p1 == 's' and p2 == 's':
print('Draw! ')
elif p1 == 'p' and p2 == 'p':
print('Draw! ')
while True:
p1 = input('Choice P1? r/p/s (Type "quit" to exit)')
p1check = p1 == 'r' or p1 == 'p' or p1 == 's'
if p1 == 'quit':
print('Bye!')
break
elif p1check is False:
print('Invalid choice P1! ')
continue
p2 = input('Choice P2? r/p/s (type "quit" to exit)')
p2check = p2 == 'r' or p2 == 'p' or p2 == 's'
if p2 == 'quit':
print('Chow!!')
break
elif p2check is False:
print('Invalid choice P2! ')
continue
rps()
答案 0 :(得分:0)
您好,欢迎来到编程的美好世界:)
您离预期的结果不远,建议您添加一个循环,直到P2输入有效为止。
#this is to avoid a while true and a break instruction
play_the_game = true
while play_the_game:
#I skip the P1 managing part for more readability
p2check = false
#Loop until the player 2 entries match the expected inputs
while p2check == false:
p2 = input('Choice P2? r/p/s (type "quit" to exit)')
p2check = p2 == 'r' or p2 == 'p' or p2 == 's'
if p2 == 'quit':
print('Chow!!')
#play_the_game is now false, the main loop will exit.
play_the_game = false
#I added 'p2check = true' to make sure the loop 'while p2check == false:' will exit.
#You can use a break instruction instead of 'p2check = true' if you prefer
p2check = true
elif p2check is False:
print('Invalid choice P2! ')
答案 1 :(得分:-1)
我建议将输入置于循环中。
ByteArrayResource resource = new ByteArrayResource(multipartFile.getBytes()){
@Override
public String getFilename() {
return systemFile.getFilenameWithExtension();
}
};
在其他语言中,这被认为是import sys
p2 = ""
while p2 != 'r' or p2 != 'p' or p2 != 's' or p2 != 'quit'
p2 = input('Choice P2? r/p/s (type "quit" to exit)')
if p2 == 'quit':
#Handle quite logic
sys.exit(0)
循环。由于Do While
是p2
,因此在第一次执行时就进入了循环。然后,如果用户输入正确的输入,则循环将中断,否则将继续询问输入。
您现在拥有的只是继续循环而无需第二次输入。