Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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-isdigit-if语句_Python_Arrays_Loops_For Loop - Fatal编程技术网

Python-isdigit-if语句

Python-isdigit-if语句,python,arrays,loops,for-loop,Python,Arrays,Loops,For Loop,我试图在main()中为设置一个循环,检查用户是否输入了数字、字符或什么都没有。如果他们输入的不是数字,我想要的输出是无效输入 本章讨论数组,我不知道如何完成 我尝试过使用isdigit(),但无法使其工作 这是我设置的主要代码,运行良好: def main(): month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'Sept

我试图在
main()
中为
设置一个
循环,检查用户是否输入了数字、字符或什么都没有。如果他们输入的不是数字,我想要的输出是无效输入

本章讨论数组,我不知道如何完成

我尝试过使用
isdigit()
,但无法使其工作

这是我设置的主要代码,运行良好:

def main():
    month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
                   'August', 'September', 'October', 'November', 'December']
    total_rain = [0] * 12 # to store the total rain per month
    sum = 0

    for i in range(12):
        total_rain[i] = int(input("Enter the total rain in " + month_names[i] + ": "))
        sum += total_rain[i] #adding rain to sum
         #printing result
    print("\n\nTotal Rainfall: " + str(sum) + "\nAverage monthly rainfall: " + str(sum/12) + "\nLowest Rainfall: " + str(min (total_rain))
          + "In month of " +str(month_names[total_rain.index(min(total_rain))])+"\nHighest Rainfall: " + str(max(total_rain))+"In month of " +
    str(month_names[total_rain.index(max(total_rain))]))

main()
在使用阵列时,如何才能做到这一点?我尝试过为
循环设置一个
,如下所示:


   for total_rain[i] in i:
       if total_rain[i].isdigit():
           print("Invalid!")
           continue
       else:
           break
但这不起作用


有人能把我推到正确的方向吗?

试试看..除了block:

try:
  total_rain[i] = int(input("Enter the total rain in " + month_names[i] + ": "))
except ValueError:
  print("Invalid!")

尝试..除块外:

try:
  total_rain[i] = int(input("Enter the total rain in " + month_names[i] + ": "))
except ValueError:
  print("Invalid!")

您可以使用
while
循环并检查有效输入

请尝试以下代码:

def main():
    month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
                   'August', 'September', 'October', 'November', 'December']
    total_rain = [0] * 12 # to store the total rain per month
    sum = 0

    for i in range(12):
        rain = None
        while not rain:  # while invalid input
            rain = input("Enter the total rain in " + month_names[i] + ": ")
            if not rain.isnumeric():  # not number
                print("Invalid!")
                rain = None  # retry input
        total_rain[i] = int(rain)
        sum += total_rain[i] #adding rain to sum
         #printing result
    s = "\n\nTotal Rainfall: " + str(sum) + "\nAverage monthly rainfall: " + str(sum/12) + "\nLowest Rainfall: " + str(min (total_rain))
          + " In month of " +str(month_names[total_rain.index(min(total_rain))])+"\nHighest Rainfall: " + str(max(total_rain))+" In month of " +
          str(month_names[total_rain.index(max(total_rain))])
    print(s)
    return s

main()
输出

Enter the total rain in January: asdfad
Invalid!
Enter the total rain in January: 
Invalid!
Enter the total rain in January: 12
Enter the total rain in February: 23
Enter the total rain in March: afad
Invalid!
Enter the total rain in March: 45
Enter the total rain in April: afcasdc
Invalid!
Enter the total rain in April: sc
Invalid!
产出2

Enter the total rain in January: 1
Enter the total rain in February: 2
Enter the total rain in March: 3
Enter the total rain in April: 4
Enter the total rain in May: 5
Enter the total rain in June: 6
Enter the total rain in July: 7
Enter the total rain in August: 8
Enter the total rain in September: 9
Enter the total rain in October: 10
Enter the total rain in November: 11
Enter the total rain in December: 12


Total Rainfall: 78
Average monthly rainfall: 6.5
Lowest Rainfall: 1 In month of January
Highest Rainfall: 12 In month of December

您可以使用
while
循环并检查有效输入

请尝试以下代码:

def main():
    month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
                   'August', 'September', 'October', 'November', 'December']
    total_rain = [0] * 12 # to store the total rain per month
    sum = 0

    for i in range(12):
        rain = None
        while not rain:  # while invalid input
            rain = input("Enter the total rain in " + month_names[i] + ": ")
            if not rain.isnumeric():  # not number
                print("Invalid!")
                rain = None  # retry input
        total_rain[i] = int(rain)
        sum += total_rain[i] #adding rain to sum
         #printing result
    s = "\n\nTotal Rainfall: " + str(sum) + "\nAverage monthly rainfall: " + str(sum/12) + "\nLowest Rainfall: " + str(min (total_rain))
          + " In month of " +str(month_names[total_rain.index(min(total_rain))])+"\nHighest Rainfall: " + str(max(total_rain))+" In month of " +
          str(month_names[total_rain.index(max(total_rain))])
    print(s)
    return s

main()
输出

Enter the total rain in January: asdfad
Invalid!
Enter the total rain in January: 
Invalid!
Enter the total rain in January: 12
Enter the total rain in February: 23
Enter the total rain in March: afad
Invalid!
Enter the total rain in March: 45
Enter the total rain in April: afcasdc
Invalid!
Enter the total rain in April: sc
Invalid!
产出2

Enter the total rain in January: 1
Enter the total rain in February: 2
Enter the total rain in March: 3
Enter the total rain in April: 4
Enter the total rain in May: 5
Enter the total rain in June: 6
Enter the total rain in July: 7
Enter the total rain in August: 8
Enter the total rain in September: 9
Enter the total rain in October: 10
Enter the total rain in November: 11
Enter the total rain in December: 12


Total Rainfall: 78
Average monthly rainfall: 6.5
Lowest Rainfall: 1 In month of January
Highest Rainfall: 12 In month of December

我调整了代码并运行了完整的测试。输出看起来正确。我调整了代码并运行了完整的测试。输出看起来是正确的。