简单python代码错误

简单python代码错误,python,Python,代码: 我的任务是:要求输入用户名。它应该存储在一个合适的变量中(如user\u name) 询问要输入多少个数字。也将其存储在适当的变量中。(如“num\u值”) 然后输入每个数字。 您需要有一些方法来保存所有数字的总数,最后,一旦输入了所有数字,您需要打印出类似的内容。问题是成行的- user_name = input("What is your name? ") print("Hello {}!, this program is going to ask you to type in a

代码:

我的任务是:要求输入用户名。它应该存储在一个合适的变量中(如
user\u name
) 询问要输入多少个数字。也将其存储在适当的变量中。(如“
num\u值
”) 然后输入每个数字。
您需要有一些方法来保存所有数字的总数,最后,一旦输入了所有数字,您需要打印出类似的内容。

问题是成行的-

user_name = input("What is your name? ")
print("Hello {}!, this program is going to ask you to type in a series of numbers (all positive integers)" .format(user_name))

total_numbers = input("How many numbers would you like to add")

for i in range(1, total_numbers):
  number = float(input("State a numbers {}: " .format(i)))
  total_numbers += number

  print("Total number: {}" .format(total_numbers))
您必须将
总数
转换为
int
,才能在
范围
功能中使用。像-

total_numbers = input("How many numbers would you like to add")
for i in range(1, total_numbers):
    number = float(input("State a numbers {}: " .format(i)))
    total_numbers += number
另外,我看到您正在向循环本身内部的
total\u numbers
添加数字,可能您想创建一个新变量,用0初始化并添加到它,增加
total\u numbers
本身会给您带来意想不到的结果

代码是-

total_numbers = int(input("How many numbers would you like to add"))
for i in range(1, total_numbers):
    number = float(input("State a numbers {}: " .format(i)))
    total_numbers += number

现在它出现了TypeError:无法将float隐式转换为str这是目前的代码:user_name=input(“您的名字是什么?”)print(“Hello{}!”,这个程序将要求您键入一系列数字(所有正整数)”。format(user_name))total_numbers=input(“您想添加多少个数字”)对于范围(1,int(总数)):数字=浮点(输入(“状态a数字{}”。格式(i)))总数+=数字打印(“总数:{}”。格式(总数))
total_numbers = int(input("How many numbers would you like to add"))
for i in range(1, total_numbers):
    number = float(input("State a numbers {}: " .format(i)))
    total_numbers += number
total_numbers = int(input("How many numbers would you like to add"))
total = 0.0
for i in range(1, total_numbers):
    number = float(input("State a numbers {}: " .format(i)))
    total += number
print("Total number: {}" .format(total))