Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Getting TypeError:参数类型为';int';在Python中是不可移植的_Python - Fatal编程技术网

Getting TypeError:参数类型为';int';在Python中是不可移植的

Getting TypeError:参数类型为';int';在Python中是不可移植的,python,Python,我想用我学到的东西写一个文字游戏,但是我遇到了一个问题 当我输入1时,这很好,但当我输入2时,它会给我以下错误: Traceback (most recent call last): File "ex36.py", line 39, in <module> start() File "ex36.py", line 27, in start if choice == 1 or "closer" in choice: TypeError: argument of

我想用我学到的东西写一个文字游戏,但是我遇到了一个问题

当我输入1时,这很好,但当我输入2时,它会给我以下错误:

Traceback (most recent call last):
  File "ex36.py", line 39, in <module>
    start()
  File "ex36.py", line 27, in start
    if choice == 1 or "closer" in choice:
TypeError: argument of type 'int' is not iterable

您的主要问题是这一行:

如果选项==1或选项中的“更接近”:
如果
choice
不是
1
,则无法在选项
中选择
“closer”而不会出现潜在错误,因为
choice
仍然可以是整数(例如数字
2
),且整数不可替代

最好保持程序简单,并保持
input()
函数的返回类型,即字符串。字符串方法
是\u digit()
已经存在,您也可以将其直接应用于返回值。在我看来,没有必要编写另一个函数来检查这一点。然后,该函数还可以返回两种不同的数据类型。这不必要地使代码复杂化

string方法
是\u digit()
已经具备了解决问题所必需的所有魔力。它允许您检查用户是否输入了数字或文本。如果要将数字保存为整数,只需将潜在数字显式转换为整数(
int(choice)

由于您有几种情况需要以不同方式处理,因此还需要编写一些
if
-语句。因此,您可以简单地执行以下操作,例如:

if not choice.isdigit():
如果选择“更接近”:
#做点什么
elif“任意”选项:
#做点什么
elif选项==“1”:
#做点什么
elif选项==“2”:
#做点什么
...
您可以使用逻辑
-运算符保存一个缩进,但这更多的是口味问题,例如:

if not choice.isdigit()和选项中的“closer”:
...
如果您只想对某些返回值做出反应,甚至可以不使用
is\u digit()
函数。但你在问题中提到,你仍然想区分数字和文本(至少我是这样理解的)。因为你总是会得到一个字符串,而这个字符串总是可编辑的。上面的错误根本不会发生



其中大部分内容你已经了解了自己,但答案也应该对其他读者有附加价值。因此,我再次修改了答案,希望能更好地澄清问题。

您的
idf
将任何数字字符串转换为数字,然后您尝试检查字符串是否为整数


您最好的选择是始终从
idf
返回字符串(或完全删除此函数),并检查
choice==“1”

choice是否为整数,2
中的
“closer”是什么意思?我想我现在明白您的意思了!有什么建议可以修改我的程序吗?如果我想让它同时接受整数和字符串作为答案,我该怎么办?我已经添加了一个答案,理想情况下,让您编写的任何函数只返回兼容类型,或者在这种情况下只返回字符串好的!我想我开始明白了。当我键入2时,程序会说choice==1为false,并继续检查choice中的“closer”,但choice是一个整数,这会产生问题。我说的对吗?@colidyre-我投了否决票,因为它感觉它没有回答任何问题,只是重新编写了错误信息。自从上次编辑尝试提供解决方案以来,我已经删除了我的否决票。是的,当我将输入作为字符串处理时,程序运行良好。我很固执,试图把事情弄复杂。谢谢你的建议@AgogAlex-不用担心,转换输入可能是一件好事,但目前您没有做任何值得这样做的操作
from sys import exit

print("""Welcome to my text game! You may input numbers (e.g. 1, 2, 3) to
navigate or type in your choice.""", "\n")

def bad_news(reason):
    print(reason)
    exit(0)

#IDs if str or int
def idf(check):
    if check.isdigit() == True:
        converted = int(check)
        return converted
    else:
        return check

def start():
    print("You're walking home down a dark road, in a dark and gloomy night.")
    print("There's someone going the same way as you.")
    print("'It's quite dark and dangerous,' you think. 'Better do something.'")
    print("Will you: 1. Follow him/her closer? or 2. Keep distance?")
    choice1 = input("> ")

    choice = idf(choice1)

    if choice == 1 or "closer" in choice:
        bad_news("""The person gets spooked and you got cops called on you.
Now you have a restraining order. Great!""")
    elif choice == 2 or "distance" in choice:
        alley()
    else:
        print("The input is invalid; please try again.")
        start()

def alley():
    print("Now in alley.")

start()