Python—将用户可编辑字符串存储到列表中的问题
我在创建一个非常简单的程序时遇到了问题,该程序允许用户存储和编辑以列表格式查看的字符串(我几周前才开始编程)。每当我执行程序时,用户输入不会编辑字符串。这是我的代码,下面是程序当前的功能,以及修补后的功能(请注意,我没有收到任何错误) 以下是我的程序所做的Python—将用户可编辑字符串存储到列表中的问题,python,string,Python,String,我在创建一个非常简单的程序时遇到了问题,该程序允许用户存储和编辑以列表格式查看的字符串(我几周前才开始编程)。每当我执行程序时,用户输入不会编辑字符串。这是我的代码,下面是程序当前的功能,以及修补后的功能(请注意,我没有收到任何错误) 以下是我的程序所做的 >>> ================================ RESTART ================================ >>> [1]hello [2] [3] [4]
>>> ================================ RESTART ================================
>>>
[1]hello
[2]
[3]
[4]
[5]
Which line would you like to edit?
> 1
You are now editing Line 1.
> hello world
[1]hello
[2]
[3]
[4]
[5]
Which line would you like to edit?
>
这是我打算让我的程序做的
>>> ================================ RESTART ================================
>>>
[1]hello
[2]
[3]
[4]
[5]
Which line would you like to edit?
> 1
You are now editing Line 1.
> hello world
[1]hello world
[2]
[3]
[4]
[5]
Which line would you like to edit?
>
如果我需要提供更多的信息,我愿意
- 雅各布·登斯莫尔
main
,所以每次重置变量时都会这样做。您需要做的是将对main
的递归调用替换为while
循环。尽可能少地更改代码,看起来是这样的(假设您在3.x上,因此使用input
):
1.每次输入main()时都(重新)定义了第1、2、3、4、5行;2.你没有使用列表,你只是使用了一堆相似命名的字符串;3.您可以将嵌套在
if
语句中的所有对main
的调用减少到末尾的单个调用中。这很有效!谢谢我很惊讶我没有看到这个问题。
>>> ================================ RESTART ================================
>>>
[1]hello
[2]
[3]
[4]
[5]
Which line would you like to edit?
> 1
You are now editing Line 1.
> hello world
[1]hello world
[2]
[3]
[4]
[5]
Which line would you like to edit?
>
def main():
line1="hello"
line2=""
line3=""
line4=""
line5=""
while True:
print("[1]"+line1)
print("[2]"+line2)
print("[3]"+line3)
print("[4]"+line4)
print("[5]"+line5)
print("")
print("Which line would you like to edit?")
lineChoice=''
while lineChoice not in ('1', '2', '3', '4', '5'):
lineChoice=input("> ")
if lineChoice=="1":
print("You are now editing Line 1.")
line1=input("> ")
if lineChoice=="2":
print("You are now editing Line 2.")
line2=input("> ")
if lineChoice=="3":
print("You are now editing Line 3.")
line3=input("> ")
if lineChoice=="4":
print("You are now editing Line 4.")
line4=input("> ")
if lineChoice=="5":
print("You are now editing Line 5.")
line5=input("> ")
main()