[英]I can't figure out how to stop this from being printed
First time on here and pretty much my first time coding, sorry if this doesn't fit but nothing I've tried has worked.第一次来这里,几乎是我第一次编码,很抱歉,如果这不合适,但我尝试过的任何方法都没有奏效。 Basically, the program is supposed to print numbers 1-100 but every time there is a multiple of 3 it prints Fizz, every 5 it prints Buzz and for both it prints FizzBuzz.
基本上,该程序应该打印数字 1-100,但每次有 3 的倍数时,它都会打印 Fizz,每 5 个它就会打印 Buzz,并且对于这两个数字,它都会打印 FizzBuzz。 At the very end of the print, it always puts 101 and I can't figure out how to stop this.
在打印的最后,它总是输入 101,我不知道如何阻止它。 The whole thing is a lot of guesswork because I don't really know what I'm doing, so help is appreciated.
整个事情都是很多猜测,因为我真的不知道自己在做什么,所以非常感谢帮助。
answer = int(1)
limit = int(100)
three: int = (3)
five: int = (5)
while answer < 100:
if limit >= answer:
if answer >= three and answer >= five:
print("FizzBuzz")
answer = answer + 1
three = three + 3
five = five + 5
if answer >= three:
print("Fizz")
answer = answer + 1
three = three + 3
if answer >= five:
print("Buzz")
answer = answer + 1
five = five + 5
if not answer >= five or answer >= three:
print (answer)
answer = answer + 1
This is the result:这是结果:
1
2
Fizz
4
Buzz
6
Fizz
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
21
Fizz
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
36
Fizz
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
51
Fizz
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
66
Fizz
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
81
Fizz
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
96
Fizz
98
Fizz
Buzz
101
When doing multiple "checks" as if
, make all of them besides the first elif
, meaning "else (the first one it attempts), if [your condition for the second one]"当像
if
一样进行多个“检查”时,除了第一个elif
之外,将所有这些都做,意思是“else(它尝试的第一个),如果 [你的第二个条件]”
It also fixes some "bugs" you had with numbers that are divisible by 3 or 5 but wouldn't show the works.它还修复了一些可以被 3 或 5 整除但不会显示作品的数字的“错误”。 Take 81 as an example, your output had the 81, mine has the word Fizz since, well, 81 is divisible by 3 (27)
以 81 为例,你的 output 有 81,我的有 Fizz 这个词,因为,81 可以被 3 整除 (27)
Change your if
statements to elif
, worked for me when I tested:将您的
if
语句更改为elif
,在我测试时为我工作:
answer = int(1)
limit = int(100)
three: int = (3)
five: int = (5)
while answer < 100:
if limit >= answer:
if answer >= three and answer >= five:
print("FizzBuzz")
answer = answer + 1
three = three + 3
five = five + 5
elif answer >= three:
print("Fizz")
answer = answer + 1
three = three + 3
elif answer >= five:
print("Buzz")
answer = answer + 1
five = five + 5
elif not answer >= five or answer >= three:
print (answer)
answer = answer + 1
My output:我的 output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Because your loop doesn't return to the start after it prints something, after it adds 1 to 100 (getting 101), it will then check the next if using answer = 101.因为您的循环在打印某些内容后不会返回开始,所以在将 1 添加到 100(得到 101)之后,如果使用 answer = 101,它将检查下一个。
You want to either use elifs instead of ifs so that only 1 if block will be executed (though you should be sure they're checked in the correct order), or a continue at the end of each if block.您想使用 elifs 而不是 ifs 以便只执行 1 个 if 块(尽管您应该确保它们以正确的顺序检查),或者在每个 if 块的末尾继续。
I suggest you shorten the sequence to 10 and then step through your code and watch what happens, especially when you get to 9. You might think there is one print
that gets run on each while
loop, but in fact print
will often be run multiple times on each while
loop.我建议您将序列缩短为 10,然后单步执行代码并观察会发生什么,尤其是当您达到 9 时。您可能认为每个
while
循环都会运行一次print
,但实际上print
通常会运行多次每个while
循环的时间。 That's because you have multiple if
statements, rather than using elif
, as other have pointed out.那是因为您有多个
if
语句,而不是像其他人指出的那样使用elif
。
Suppose you reach the state at which假设您到达 state
limit is 100限制为 100
answer is 99答案是 99
five is 100五是100
three is 99三是99
Your while condition and first if condition are both true.您的 while 条件和 first if 条件都为真。
while answer < 100:
if limit >= answer:
Then the next if
condition is tested:然后测试下一个
if
条件:
if answer >= three and answer >= five:
Not true ( answer
is less than five
).不正确(
answer
少于five
)。
But then you hit the next if
:但是然后你点击下一个
if
:
if answer >= three:
print("Fizz")
answer = answer + 1
three = three + 3
The condition is true, and so this block is run.条件为真,因此运行此块。 After the
print
, answer
is incremented to 100, and three
is incremented to 102.在
print
之后, answer
递增到 100,而three
递增到 102。
Since you didn't use elif
s, you continue through the while
loop block.由于您没有使用
elif
,因此您将继续执行while
循环块。 So, the next if
is tested:因此,测试下一个
if
:
if answer >= five:
print("Buzz")
answer = answer + 1
five = five + 5
The condition is true, so that block is run.条件为真,因此该块运行。 After the
print
, answer
is incremented to 101, and five
is incremented to 105.在
print
之后, answer
递增到 101,而five
递增到 105。
Finally, you get to the last if
in the while
loop:最后,您将到达
while
循环中的最后一个if
:
if not answer >= five or answer >= three:
print (answer)
answer = answer + 1
At this point, you're not comparing answer
with limit
or with 100
.此时,您没有将
answer
与limit
或100
进行比较。 That's because you assumed that was happening in the while
statement.那是因为您假设这发生在
while
语句中。 This condition is true and so this block will get run.此条件为真,因此该块将运行。 Hence
print(101)
.因此
print(101)
。
As suggested by others, changing to elif
s will fix things.正如其他人所建议的那样,更改为
elif
将解决问题。
But there are other improvements you can make但是您还可以进行其他改进
for i in range(1, 101): # start, stop -- stop won't be included
if i % 5 == 0 and i % 3 == 0:
print("FizzBuzz")
elif i % 5 == 0:
print("Buzz")
elif i % 3 == 0:
print("Fizz")
else:
print(i)
Note that you don't have to use int(n)
to assign a number.请注意,您不必使用
int(n)
来分配数字。 You only need to use int()
to convert a float or number string to an integer.您只需使用
int()
将浮点数或数字字符串转换为 integer。
The range(1, 101)
function generates a sequence from 1 to 100, and the for
statement will loop once for each value in the range. range(1, 101)
function 生成一个从 1 到 100 的序列, for
语句会为范围内的每个值循环一次。 You don't need to increment anything within the loop.您不需要在循环中增加任何内容。
%
is the modulo operator—it gives the remainder after division. %
是取模运算符——它给出除法后的余数。 For example, 9 % 5
equals 4;例如,
9 % 5
等于 4; 10 % 5
equals 0. So if i % 5 == 0
will be true whenever i
is a multiple of 5. 10 % 5
等于 0。所以if i % 5 == 0
将在i
是 5 的倍数时为真。
The if... elif
pattern ensures that only one of the nested blocks is executed on each loop of the for
loop. if... elif
模式确保在for
循环的每个循环中只执行一个嵌套块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.