【问题标题】:Python3.4: Nested Functions Not Executing [duplicate]Python3.4:嵌套函数不执行[重复]
【发布时间】:2014-06-08 07:09:50
【问题描述】:

这是我的完整代码:

import sys
import string

version = 1.0
print ("...Written in Python 3.4...\n")
print ("Copyright 2014 Ethan Gold. version 1.0")
print ("\n")
print ("Welcome to BattleText!")
print ("Is this your first time playing?")
#functions:



            #Story Select
def storySelect(sys, string):
    print ("\n Story Select: 1: Beta Framework")
    yn0 = input("> ")
    if yn0 == 1:
        betFrame(sys, string)

            #Beta Framework
def betFrame(sys, string):
    print ("test")


yn = input("> ")
if yn == "y":
    print ("\n Welcome to the world of BattleText!")
    print ("BT is a combat-based text adventure platform that supports multiple stories")
    print ("In this game, you win by defeating all enemies. You lose if your health drops to 0\n")
    print ("This is version", version)
    if version == 1.0:
        print ("This version of BattleText does not support third party or patchable stories")
        print ("BTv1.0 is a demo and all stories are distributed through official updates")
        print ("This is a test beta framework")
    else:
        print ("This version of BT should support third-party and patchable stories")

else:
    storySelect(sys, string)

出现问题的部分在这里:

#Story Select
    def storySelect(sys, string):
        print ("\n Story Select: 1: Beta Framework")
        yn0 = input("> ")
        if yn0 == 1:
            betFrame(sys, string)

                #Beta Framework
    def betFrame(sys, string):
        print ("test")

当 storySelect 被调用时,它应该要求你从一个列表中选择。当您键入 1 时,它应该调用 betFrame,但它似乎没有这样做。相反,当我输入 1 时,它只是变为空白,程序退出时没有错误。请帮忙!

【问题讨论】:

    标签: python function python-3.x nested


    【解决方案1】:

    input 返回一个字符串。您将其与整数进行比较,因此表达式永远不会为真。您需要将输入转换为 int(或与字符串进行比较)。

    yn0 = input("> ")
    if yn0 == "1":
        # do stuff
    
    
    yn0 = int(input("> ")) # Also consider catching a ValueError exception
    if yn0 == "1":
        # do stuff
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    相关资源
    最近更新 更多