Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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
我做错了什么?尝试在Python中使用_Python - Fatal编程技术网

我做错了什么?尝试在Python中使用

我做错了什么?尝试在Python中使用,python,Python,请阅读我的代码,以便更好地理解我的问题。我正在用python创建一个待办事项列表。在有try和except的while循环中,我想将用户输入类型设置为string。如果用户输入一个整数,我想在“except”块中打印消息。但是,如果我在运行代码时键入整数,它不会执行ValueError 代码如下: to_do_list = [] print(""" Hello! Welcome to your notes app. Type 'SHOW' to show your list so far

请阅读我的代码,以便更好地理解我的问题。我正在用python创建一个待办事项列表。在有try和except的while循环中,我想将用户输入类型设置为string。如果用户输入一个整数,我想在“except”块中打印消息。但是,如果我在运行代码时键入整数,它不会执行ValueError

代码如下:

to_do_list = []


print("""

Hello! Welcome to your notes app.

Type 'SHOW' to show your list so far
Type 'DONE' when you'v finished your to do list

""")

#let user show their list
def show_list():
    print("Here is your list so far: {}. Continue adding below!".format(", ".join(to_do_list)))

#append new items to the list
def add_to_list(user_input):
    to_do_list.append(user_input)
    print("Added {} to the list. {} items so far".format(user_input.upper(), len(to_do_list)))

#display the list
def display_list():
    print("Here's your list: {}".format(to_do_list))

print("Enter items to your list below")  
while True:

    #HERE'S WHERE THE PROBLEM IS!

    #check if input is valid
    try:
        user_input = str(input(">"))
    except ValueError:
        print("Strings only!")
    else:    

        #if user wants to show list
        if user_input == "SHOW":
            show_list()
            continue
        #if user wants to end the list
        elif user_input == "DONE":
            new_input = input("Are you sure you want to quit? y/n ")
            if new_input == "y":
                break
            else:
                continue

        #append items to the list
        add_to_list(user_input)


display_list()

你的假设有两个问题:

  • 对整数调用
    str
    不会引发
    ValueError
    ,因为每个整数都可以表示为字符串
  • input
    返回的所有内容(不管怎样,在Python 3上,看起来您正在使用)都已经是一个字符串。将字符串转换为字符串肯定不会抛出错误
  • 如果您想扔掉所有数字输入,可能需要使用



    关于“全数字”一词的评论似乎有些混乱。我指的是一个完全由数字组成的字符串,这是我对OP在待办事项列表中不需要“整数”的解释。如果你想扔掉一些更广泛的字符串化数字(有符号整数、浮点数、科学记数法),
    isdigit
    不适合你

    你的假设有两个问题:

  • 对整数调用
    str
    不会引发
    ValueError
    ,因为每个整数都可以表示为字符串
  • input
    返回的所有内容(不管怎样,在Python 3上,看起来您正在使用)都已经是一个字符串。将字符串转换为字符串肯定不会抛出错误
  • 如果您想扔掉所有数字输入,可能需要使用



    关于“全数字”一词的评论似乎有些混乱。我指的是一个完全由数字组成的字符串,这是我对OP在待办事项列表中不需要“整数”的解释。如果你想扔掉一些更广泛的字符串化数字(有符号整数、浮点数、科学记数法),
    isdigit
    不适合你

    input
    返回一个字符串。有关
    输入
    功能,请参阅。将此函数的结果强制转换为字符串不会起任何作用

    您可以使用检查字符串是否为数字

    if user_input.isdecimal():
        print("Strings only!")
    

    这与您现有的
    else
    子句非常吻合。

    输入
    返回一个字符串。有关
    输入
    功能,请参阅。将此函数的结果强制转换为字符串不会起任何作用

    您可以使用检查字符串是否为数字

    if user_input.isdecimal():
        print("Strings only!")
    

    这将非常适合您现有的
    else
    子句。

    在Python中,
    input
    始终返回字符串。例如:

    >>> input('>')
    >4
    '4'
    
    所以在这种情况下,
    str
    不会抛出ValueError——它已经是一个字符串了


    如果您真的想检查并确保用户没有只输入数字,您可能想检查您的输入是否都是数字,然后出错。

    在Python中,
    input
    总是返回字符串。例如:

    >>> input('>')
    >4
    '4'
    
    所以在这种情况下,
    str
    不会抛出ValueError——它已经是一个字符串了


    如果您真的想检查并确保用户没有只输入数字,您可能想检查您的输入是否都是数字,然后出错。

    str([integer value])
    是值,不会抛出错误-您只需将输入(已经是字符串)转换为字符串。
    str([integer value])
    是值,不会抛出错误-您只需将输入(已经是字符串)转换为字符串。要抛出数字输入,最好的方法是
    尝试
    将其转换为某种数字(并处理该异常)。。。e、 例如,
    isdigit
    不会处理
    -50
    isdigit
    不会找到所有数字(仅字符串格式的整数)!要扔掉数字输入,最好的方法是
    尝试
    将其转换为某种数字(并处理该异常)。。。e、 例如,
    isdigit
    不会处理
    -50
    isdigit
    不会找到所有数字(仅字符串格式的整数)!