Python全局变量不会循环更新

时间:2019-11-28 19:49:12

标签: python

这不是家庭作业。刚刚决定参加一个有趣的活动,将简单的数学问题整理成代码。我的total_daiagonals变量保持为0。令我惊讶的是,所有执行都在全局范围内。这是完整的代码段,因此您可以按原样在计算机上运行它。

#!/usr/bin/env python

print("Enter a number of sides you are having: ")
sides = input()

# Minimum number of sides for a formula to make sence mathematically
sides_index = 3

diagonals_increment = 2

total_diagonals = 0

print("Starting a loop")
for sides in range(sides_index, sides):
    print("----------------------------------")
    if sides_index > sides:
        print("Formatting output...")
        print("The number of diagonals is: %d" % total_diagonals)
    else:
        sides_index = sides_index + 1
        total_daiagonals = total_diagonals + diagonals_increment
        diagonals_increment = diagonals_increment + 1
        print("sides_index: %d, total_diagonals: %d, diagonals_increment: %d" % (sides_index, total_diagonals, diagonals_increment))

输出:

➜ ./diagonal_calculator.py
Enter a number of sides you are having:
6
Starting a loop
----------------------------------
sides_index: 4, total_diagonals: 0, diagonals_increment: 3
----------------------------------
sides_index: 5, total_diagonals: 0, diagonals_increment: 4
----------------------------------
sides_index: 6, total_diagonals: 0, diagonals_increment: 5

2 个答案:

答案 0 :(得分:1)

您的代码有两个问题,首先是@ kaya3指出的,其中有一个错字。另一个问题是input将被解释为字符串,并且在Typerror函数中使用时将生成range(),这就是为什么我使用int(input())的原因。从命令行看来,这对我来说工作正常,因此对生产来说看起来不错:

print("Enter a number of sides you are having: ")
sides = int(input())

# Minimum number of sides for a formula to make sence mathematically
sides_index = 3

diagonals_increment = 2

total_diagonals = 0

print("Starting a loop")
for sides in range(sides_index, sides):
    print("----------------------------------")
    if sides_index > sides:
        print("Formatting output...")
        print("The number of diagonals is: %d" % total_diagonals)
    else:
        sides_index = sides_index + 1
        total_diagonals = total_diagonals + diagonals_increment
        diagonals_increment = diagonals_increment + 1
        print("sides_index: %d, total_diagonals: %d, diagonals_increment: %d" % (sides_index, total_diagonals, diagonals_increment))

答案 1 :(得分:0)

看看以下内容是否有帮助,我编辑了您的代码以使其更简洁:

#!/usr/bin/env python

sides = int(input("Enter a number of sides you are having: "))

# Minimum number of sides for a formula to make sence mathematically
sides_index = 3
diagonals_increment = 2
total_diagonals = 0

print("Starting a loop")
for sides in range(sides_index, sides):
    print("----------------------------------")
    if sides_index > sides:
        print("Formatting output...\nThe number of diagonals is: {}".format(total_diagonals))
    else:
        sides_index += 1
        total_diagonals += diagonals_increment
        diagonals_increment += 1
        print("sides_index: {}, total_diagonals: {}, diagonals_increment: {}".format(sides_index, total_diagonals, diagonals_increment))

print(sides_index, total_diagonals, diagonals_increment)

输出:

Enter a number of sides you are having: 6
Starting a loop
----------------------------------
sides_index: 4, total_diagonals: 2, diagonals_increment: 3
----------------------------------
sides_index: 5, total_diagonals: 5, diagonals_increment: 4
----------------------------------
sides_index: 6, total_diagonals: 9, diagonals_increment: 5
6 9 5