Python 声明数字和总数的变量

Python 声明数字和总数的变量,python,Python,每次我运行时,它都会显示“一行接一行的意外字符延续字符” 我做错了什么???更改此行: # Declare variables for the number, and the # total. def main(): # Initialize for while loop number = 1.0 total = 0.0 # Continue adding numbers while they are positive. while number > 0: nu

每次我运行时,它都会显示“一行接一行的意外字符延续字符”

我做错了什么???

更改此行:

# Declare variables for the number, and the
# total.

def main():
# Initialize for while loop
    number = 1.0
    total = 0.0

# Continue adding numbers while they are positive.
while number > 0:
    number = float(input('Enter a positive number' \ ' (negative to quit): '))

    # Check that number is positive so as
    # not to change value of the total.
    if number > 0:
        total != total + number
    if number > 0:
        total == total + number

# Display total.
print ('Total =', format.(total, '.2f'))

main()
number = float(input('Enter a positive number (negative to quit): '))
也许是为了:

number = float(input('Enter a positive number' \ ' (negative to quit): '))
属于
main()
一部分的所有内容都应一致缩进(例如,使用4个空格)

另外,更改此行:

# Declare variables for the number, and the
# total.

def main():
# Initialize for while loop
    number = 1.0
    total = 0.0

# Continue adding numbers while they are positive.
while number > 0:
    number = float(input('Enter a positive number' \ ' (negative to quit): '))

    # Check that number is positive so as
    # not to change value of the total.
    if number > 0:
        total != total + number
    if number > 0:
        total == total + number

# Display total.
print ('Total =', format.(total, '.2f'))

main()
number = float(input('Enter a positive number (negative to quit): '))
为此:

total == total + number
更改此行:

# Declare variables for the number, and the
# total.

def main():
# Initialize for while loop
    number = 1.0
    total = 0.0

# Continue adding numbers while they are positive.
while number > 0:
    number = float(input('Enter a positive number' \ ' (negative to quit): '))

    # Check that number is positive so as
    # not to change value of the total.
    if number > 0:
        total != total + number
    if number > 0:
        total == total + number

# Display total.
print ('Total =', format.(total, '.2f'))

main()
number = float(input('Enter a positive number (negative to quit): '))
也许是为了:

number = float(input('Enter a positive number' \ ' (negative to quit): '))
属于
main()
一部分的所有内容都应一致缩进(例如,使用4个空格)

另外,更改此行:

# Declare variables for the number, and the
# total.

def main():
# Initialize for while loop
    number = 1.0
    total = 0.0

# Continue adding numbers while they are positive.
while number > 0:
    number = float(input('Enter a positive number' \ ' (negative to quit): '))

    # Check that number is positive so as
    # not to change value of the total.
    if number > 0:
        total != total + number
    if number > 0:
        total == total + number

# Display total.
print ('Total =', format.(total, '.2f'))

main()
number = float(input('Enter a positive number (negative to quit): '))
为此:

total == total + number
键入时:

total = total + number
反斜杠将自动读取为一个行连续字符,python希望该字符后面紧跟n表示换行符。

当您键入:

total = total + number

反斜杠被自动读取为行连续字符,python希望换行符后跟n。

为什么不将
/
放在引号中呢?将赋值也固定到
总计
<代码>总计!=total+number
total==total+number
只是返回true或false的布尔运算,但不会更改存储在
total
中的值。如果你真的打算做一个比较,你应该知道这可能行不通。谷歌“python浮点相等”获取更多信息。同样的条件表达式也有两次。其中一个是输入错误吗?直到
while
循环执行之后,您的主函数才会被调用,而且它初始化的变量是它的局部变量,而不是
while
循环使用的全局变量。您为什么不将
/
放在引号中呢?同时修复
total
的赋值<代码>总计!=total+number
total==total+number
只是返回true或false的布尔运算,但不会更改存储在
total
中的值。如果你真的打算做一个比较,你应该知道这可能行不通。谷歌“python浮点相等”获取更多信息。同样的条件表达式也有两次。一个是输入错误吗?你的主函数直到
while
循环执行后才被调用,它初始化的变量是它的局部变量,而不是
while
循环使用的全局变量。我想他想要这样的东西:
输入一个正数\(负则退出):“
并在双引号中加上反斜杠。这确实是我看到他使用反斜杠的唯一原因。或者:
number=float(输入('enta positive number\n(negative to quit):'))
Traceback(最近一次调用):文件“F:/computer program/last assignment/assignment2.py”,第26行,打印('Total=',format(Total),.2f'))NameError:name'total'未定义>>>它一直在说我是如何不定义itI的我更新了我的答案。另请参见上面对原始问题的评论-它们很有用。我想说他想要这样的东西:
“输入一个正数\(如果退出则为负数):”
,并在双引号中加上反斜杠。这确实是我看到他使用反斜杠的唯一原因。或者:
number=float(输入('enta positive number\n(negative to quit):'))
Traceback(最近一次调用):文件“F:/computer program/last assignment/assignment2.py”,第26行,打印('Total=',format(Total),.2f'))NameError:name'total'未定义>>>它一直在说我是如何不定义itI的我更新了我的答案。另请参见以上对原始问题的评论-它们很有帮助。