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()

0

3个回答

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

0
weixin_42246860
samyyc 谢谢,是我把简单难度写成了简单模式,已采纳
11 个月之前 回复

建议在输入选择条件时先用数字或者字母进行判断,看是否逻辑正确。你直接来中文,但中文输入可能存在编码不统一问题的(比如你内容要求是utf-8,环境传来的是gbk)

0

键盘输入的中文可能不是utf-8编码的,支持中文的也可能是GBK,GB12

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!