首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在“幽灵游戏”中增加一个获胜的功能

在“幽灵游戏”中增加一个获胜的功能
EN

Stack Overflow用户
提问于 2022-09-10 23:43:57
回答 1查看 28关注 0票数 -2

最近,我重新开始编程,并决定在鬼游戏中添加一个特性,您可以看到下面的原始代码。

代码语言:javascript
复制
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)

既然你只能在这场比赛中输掉,我想我会增加一个赢球的方法。

代码语言:javascript
复制
#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语句,任何提示都将不胜感激(:

EN

回答 1

Stack Overflow用户

发布于 2022-09-11 00:57:53

我相信下面的代码片段和经过调整的if/ will测试将产生所需的游戏场景。

代码语言:javascript
复制
#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)

我绕了几步,以增加分数,并测试一场胜利。下面是一些玩过的游戏样本。

代码语言:javascript
复制
@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

试试看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73675887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档