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

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 帮我解决一下膳食平衡的线性规划模型的数据实例
  • ¥40 万年历缺少农历,需要和阳历同时显示
  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因