最近,我重新开始编程,并决定在鬼游戏中添加一个特性,您可以看到下面的原始代码。
from random import randint
print('Ghost Game')
feeling_brave=True
score=0
while feeling_brave:
ghost_door = randint(1, 3)
print('Three doors ahead')
print('A ghost behind one.')
print('Which door do you open?')
door = input('1, 2, or 3?')
door_num = int(door)
if door_num == ghost_door:
print('GHOST!')
feeling_brave = False
else:
print('No ghost!')
print('Y0u enter the next room.')
score = score + 1
print('Run Away!')
print('Game over! You scored', score)既然你只能在这场比赛中输掉,我想我会增加一个赢球的方法。
#Ghost Game
from random import randint
print('Ghost Game')
feeling_brave=True
score=0
while feeling_brave:
ghost_door = randint(1, 3)
print('Three doors ahead')
print('A ghost behind one.')
print('Which door do you open?')
door = input('1, 2, or 3?')
door_num = int(door)
if door_num == ghost_door:
print('GHOST!')
feeling_brave = False
else:
print('No ghost!')
print('You enter the next room.')
score = score + 1
if score == 2:
print ('You Won!')
print('Run Away!')
print('Game over! You scored', score)我理解这个问题,如果其他语句是一个循环,如果我使用一个中断语句(失败结束也是这样),以及如果分数为2时仍在播放的if score == 2语句,但是我想打破循环,只打印you won语句,任何提示都将不胜感激(:
发布于 2022-09-11 00:57:53
我相信下面的代码片段和经过调整的if/ will测试将产生所需的游戏场景。
#Ghost Game
from random import randint
print('Ghost Game')
score=0
while True: # Just used "True" in lieu of a boolean variable
ghost_door = randint(1, 3)
print('Three doors ahead')
print('A ghost behind one.')
print('Which door do you open?')
door = input('1, 2, or 3?')
door_num = int(door)
if door_num == ghost_door:
print('GHOST! = Sorry, you lost')
break
else:
print('No ghost!')
score = score + 1
if score == 2:
print('You Won! - Run Away!')
break
print('You enter the next room.')
print('Game over! You scored', score)我绕了几步,以增加分数,并测试一场胜利。下面是一些玩过的游戏样本。
@Una:~/Python_Programs/Ghost$ python3 Ghost.py
Ghost Game
Three doors ahead
A ghost behind one.
Which door do you open?
1, 2, or 3?1
No ghost!
You enter the next room.
Three doors ahead
A ghost behind one.
Which door do you open?
1, 2, or 3?2
No ghost!
You Won! - Run Away!
Game over! You scored 2
@Una:~/Python_Programs/Ghost$ python3 Ghost.py
Ghost Game
Three doors ahead
A ghost behind one.
Which door do you open?
1, 2, or 3?1
GHOST! = Sorry, you lost
Game over! You scored 0试试看。
https://stackoverflow.com/questions/73675887
复制相似问题