gpt4 book ai didi

Python 不太难,只是在变量方面遇到困难

转载 作者:行者123 更新时间:2023-12-01 05:16:52 26 4
gpt4 key购买 nike

代码:

loop = 0

def main():
while loop == 0:
Num = input("Please Enter The Number Of People That Need The Cocktails ")
print()
print(" Type END if you want to end the program ")
print()
for count in range (Num):
with open("Cocktails.txt",mode="w",encoding="utf-8") as myFile:
print()
User = input("Please Enter What Cocktails You Would Like ")
if User == "END":
print(Num, "Has Been Written To The File ")
exit()
else:
myFile.write(User+"/n")
myFile.write(Num+"/n")
print()
print(User, "Has Been Written To The File ")

错误:

line 9, in main for count in range (Num): TypeError: 'str' object cannot be interpreted as an integer

我试图将变量设置为重复他们想要多少鸡尾酒的次数。

示例:

How many cocktails ?  6 

然后,脚本应要求用户输入他想要的鸡尾酒六次。

最佳答案

在 Python 中,input() 默认返回一个字符串。将 Num 更改为:

Num = int(input("Please Enter The Number Of People That Need The Cocktails ")) 

还有

MyFile.write(Num + "\n")  

应阅读:

MyFile.write(str(Num) + "\n")

仅供记录,您可以替换:

loop = 0
while (loop == 0):

与:

while True:

关于Python 不太难,只是在变量方面遇到困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23021679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com