samyyc 2018-06-21 14:13 采纳率: 0%
浏览 3419
已采纳

Python if语句不输出

下面是我的代码,主要在于points函数,它不执行if里面的内容,但也没有报错

# -*- coding:utf-8 -*-
import random
import time
import sys

playerPoints = 100
playerWon = False
playerChoice = None
playerGuess = None

def easy():
    ## 简单难度的踩雷,在 1 ~ 100 之间随机选择雷号
    global playerWon
    rbnList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,
               37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,
               70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
    rbn = random.sample(rbnList,3)
    if playerGuess == rbn[0] or playerGuess == rbn[1] or playerGuess == rbn[2]:
        sys.stdout.write("你踩到地雷了!")
        playerWon = False
        points()
    else:
        sys.stdout.write("你躲开了地雷!")
        playerWon = True
        points()

def normal():
    ## 正常难度的踩雷,在 1 ~ 50 之间随机选择雷号
    global playerWon
    rbnList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,
               37,38,39,40,41,42,43,44,45,46,47,48,49,50]
    rbn = random.sample(rbnList,3)
    if playerGuess == rbn[0] or playerGuess == rbn[1] or playerGuess == rbn[2]:
        sys.stdout.write("你踩到地雷了!")
        playerWon = False
    else:
        sys.stdout.write("你躲开了地雷!")
        playerWon = True
    points()

def hard():
    ## 困难难度的踩雷,在 1 ~ 20 之间随机选择雷号
    global playerWon
    rbnList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
    rbn = random.sample(rbnList, 3)
    if playerGuess == rbn[0] or playerGuess == rbn[1] or playerGuess == rbn[2]:
        sys.stdout.write("你踩到地雷了!")
        playerWon = False
    else:
        sys.stdout.write("你躲开了地雷!")
        playerWon = True
    points()

def points():
    ## 玩家分数,踩雷游戏分数系统
    global playerPoints
    if playerWon == True:
        if playerChoice == "简单难度":
            playerPoints += 5
            sys.stdout.write("当前分数加5, 当前分数: %s" % playerPoints)
        elif playerChoice == "正常难度":
            playerPoints += 10
            sys.stdout.write("当前分数加10, 当前分数: %s" % playerPoints)
        elif playerChoice == "困难难度":
            playerPoints += 20
            sys.stdout.write("当前分数加20, 当前分数: %s" % playerPoints)
    elif playerWon == False:
        if playerChoice == "简单难度":
            playerPoints -= 5
            sys.stdout.write("当前分数减5, 当前分数: %s" % playerPoints)
            if playerPoints < 0:
                sys.stdout.write("分数过少,不能进行减分,请等待分数重置")
                playerPoints = 0
                resetPoints()
        elif playerChoice == "正常难度":
            playerPoints -= 7
            sys.stdout.write("当前分数减7, 当前分数: %s" % playerPoints)
            if playerPoints < 0:
                sys.stdout.write("分数过少,不能进行减分,请等待分数重置")
                playerPoints = 0
                resetPoints()
        elif playerChoice == "困难难度":
            playerPoints -= 10
            sys.stdout.write("当前分数减10, 当前分数: %s" % playerPoints)
            if playerPoints < 0:
                sys.stdout.write("分数过少,不能进行减分,请等待分数重置")
                playerPoints = 0
                resetPoints()

def resetPoints():
    resetPointsTimes = 300
    for i in range(resetPointsTimes):
        sys.stdout.write("分数将在 {} 秒后重置为100".format(resetPointsTimes - i))
        sys.stdout.flush()
        time.sleep(1)
    sys.stdout.write("分数成功重置为100")
    GameBody()

def rules():
    sys.stdout.write("==================== *游戏规则* ====================")
    sys.stdout.write("本游戏分3个模式:简单模式,正常模式和困难模式")
    sys.stdout.write("简单模式: 赢了得5分, 输了扣5分")
    sys.stdout.write("正常模式: 赢了得10分, 输了扣7分")
    sys.stdout.write("困难模式: 赢了得20分, 输了扣10分")
    sys.stdout.write("祝您游戏愉快!")

def GameBody():
    global playerChoice
    global playerGuess

    playerChoice = input("简单模式请输入'简单模式',正常模式请输入'正常模式'," +
                         "困难模式请输入'困难模式',查看游戏规则请输入'规则':")
    if playerChoice == "简单模式":
        playerGuess = int(input("请输入一个 1 ~ 100 的数字"))
        easy()
    elif playerChoice == "正常模式":
        playerGuess = int(input("请输入一个 1 ~ 50 的数字"))
        normal()
    elif playerChoice == "困难模式":
        playerGuess = int(input("请输入一个 1 ~ 20 的数字"))
        hard()
    elif playerChoice == "规则":
        rules()
    else:
        sys.stdout.write("输入错误, 请重新输入")
        GameBody()

GameBody()

  • 写回答

3条回答 默认 最新

  • threenewbee 2018-06-21 14:29
    关注

    你的输入是简单模式、正常模式等等么?你的控制台支持中文么?输入中有别的自负,比如空格么?
    建议你换成123 abc等测试下,而不是中文。
    再不行的话,单步调试下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 如何解决智能小车直道抖动
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站