gpt4 book ai didi

python - Project Euler/Python : find sum of multiples of 3 and 5. 程序未继续输入

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:48:05 26 4
gpt4 key购买 nike

我是编程新手,我正在参加 Project Euler 挑战赛,让我有理由学习。

在下面找到我非常简单的 python 代码

x = 1                       
thirdDivide = 0
fifthDivide=0
total = 0

print('Enter the max value')

maxValue = input()

while (x != maxValue):
thirdDivide = x / 3
fifthDivide = x / 5
if (thirdDivide).is_integer():
total = total + x
x = x + 1
elif (fifthDivide).is_integer():
total = total + x
x = x + 1

print ("The sum of the multiples of 3 and 5 between 0 and " + maxValue + " is " + total)

当我运行它时,它会询问我的最大值,然后停止执行任何操作。

谢谢!

最佳答案

假设您在 Python 3 中,使用字符串代替 float ,或使用 float 代替字符串,无限循环的修复如下:

x = 1
thirdDivide = 0
fifthDivide=0
total = 0

maxValue = float(input('Enter the max value: '))

while (x != maxValue):
thirdDivide = x / 3
fifthDivide = x / 5
if (thirdDivide).is_integer():
total = total + x
elif (fifthDivide).is_integer():
total = total + x
x = x + 1

print("The sum of the multiples of 3 and 5 between 0 and " + str(maxValue) + " is " + str(total))

请注意,我不会检查您的算法是否正确以及它是否计算出它应该做什么。但现在它会产生一些结果并进行编译。

关于python - Project Euler/Python : find sum of multiples of 3 and 5. 程序未继续输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40370515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com