如何解决在我的小项目中遇到了创建函数的问题
目前,我正在制作一个项目,并且想向其中添加一个功能。但是我对最近遇到的问题一无所知。因此,这是我编写的函数的错误代码:
user_input = input()
while True:
if user_input == "add_1":
print("Start adding your #1 note...")
content_1 = input("Please enter content: ")
elif content_1 != '':
print("You have added the #1 note. Please use other functions.")
user_input = input()
if user_input == "add_2":
print("Start adding your #2 note...")
content_2 = input("Please enter content: ")
elif content_2 != '':
print("You have added the #2 note. Please use other functions.")
user_input = input()
if user_input == "add_3":
print("Start adding your #3 note...")
content_3 = input("Please enter content: ")
elif content_3 != '':
print("You have added the #3 note. Please use other functions.")
user_input = input()
预期结果: 当用户输入字符串“ add_1”时, 系统将处理:
开始添加您的#1笔记... (下一行)请输入内容:(例如,在此处写一些东西)
然后,用户输入的内容将存储到变量'content_1'。
接下来,在变量“ content_1”不是“空”之后,当用户输入字符串“ add_1”时,系统将打印消息,“您已添加#1注释。请使用其他功能。'
系统将根据其他输入“ add_2”和“ add_3”执行类似的功能。
现在的情况是我想不出为该函数编写合适的代码的解决方案。一些编程大师可以帮助我解决这个问题吗?非常感谢。
解决方法
代码混乱且难以阅读,我建议在打开计算机并编写第一行代码之前勾勒出diagram的逻辑工作原理。
问题出在嵌套的if / elif语句内。对于每个选项("add_1"
,"add_2"
,"add_3"
),应该有一个while
循环,只要content_1
不为空,循环就应该运行。内循环完成后,外循环重新启动。
代码应如下所示。其他选项应该以相同的方式工作。
while True:
# take user input at the start of the loop
user_input = input()
# option 1
if user_input == "add_1":
print("Start adding your #1 note...")
content_1 = input("Please enter content: ")
# while content_1 is '',ask again
while content_1 == '':
print("Start adding your #1 note...")
content_1 = input("Please enter content: ")
print("You have added the #1 note. Please use other functions.")
# similarly for other options
elif user_input == "add_2":
# ...
进一步阅读:
- 在某些情况下,新添加的
walrus operator (:=)
在检查用户输入时很有用。
有关更多信息,请访问this教程
我不确定您要做什么,但让我们尝试找到解决方法
while True:
user_input = input('What function do you want to use: ')
if user_input == "add_1":
print("Start adding your #1 note…")
content_1 = input("Please enter content: ")
if content_1 != '':
print("You have added the #1 note. Please use other functions.")
elif user_input == "add_2":
print("Start adding your #2 note…")
content_2 = input("Please enter content: ")
if content_2 != '':
print("You have added the #2 note. Please use other functions.")
elif user_input == "add_3":
print("Start adding your #3 note…")
content_3 = input("Please enter content: ")
if content_3 != '':
print("You have added the #3 note. Please use other functions.")
但是我们可以简化此解决方案:
content = {}
while True:
user_input = input('What function do you want to use: ')
if user_input.startswith("add_") and len(user_input) == 5:
note_number = user_input[4]
if note_number in ('1','2','3'):
print(f"Start adding your #{note_number} note…")
content[note_number] = input("Please enter content: ")
if content[note_number]:
print(f"You have added the #{note_number} note. Please use other functions.")
您想做吗?
,如果您的意思是将user_input将content_1更改为> user_input,则未定义content_1
elif content_1!='':
print(“您已添加#1注释。请使用其他功能。”) user_input = input()
您有类似的多个错误
if / elif语句最后必须具有else语句:
如果(条件):
声明 elif(条件): 陈述书 其他: 声明
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。