Python FizzBuzz
已经给我这个问题要在Python中完成:
I have been given this question to do in Python:
从用户那里获取一个数字列表,然后在该列表上运行FizzBuzz.
Take in a list of numbers from the user and run FizzBuzz on that list.
当您遍历列表时,请记住以下规则:
When you loop through the list remember the rules:
1)如果数字可以被3和5整除,请打印FizzBuzz
1) If the number is divisible by both 3 and 5 print FizzBuzz
2)如果只能被3打印整除Fizz
2) If it's only divisible by 3 print Fizz
3)如果只能被5整除Buzz
3) If it's only divisible by 5 print Buzz
4)否则只需打印数字
4) Otherwise just print the number
还记得小精灵!
我创建了以下脚本,但是如果n%3=True
I have the following script created, but it gives me an error at if n%3=True
n=input()
if n%3=True:
print("Fizz")
else if n%5=True:
print ("Buzz")
elif print n
任何人都可以帮忙吗?非常感谢你!
Can anyone help? Thank you very much!
此处的代码存在一些问题.第一个问题是,为了进行比较,您应该使用==
,而不是=
,它是用于赋值的.
A few issues with your code here. The first issue is that, for comparison, you should be using ==
, not =
, which is for assignment.
第二个问题是您要检查除法的余数(这是模运算符的计算结果)是否为 zero ,而不是 true ,即真的没有道理.
The second issue is that you want to check that the remainder of the divisions (which is what the modulo operator calculates) is zero, not that it's true, which doesn't really make sense.
您应该将elif
用作否则,如果...",将else
用作否则".并且您需要修复else
子句的格式.
You should be using elif
for "otherwise if..." and else
for "otherwise." And you need to fix the formatting of your else
clause.
您要:
n=input()
if n%3 == 0:
print("Fizz")
elif n%5 == 0:
print ("Buzz")
else:
print n
最后,您的代码不符合规范:
Finally, your code does not meet the spec:
1)如果数字可以被3和5整除,则打印"FizzBuzz"
1) If the number is divisible by both 3 and 5 print "FizzBuzz"
以上内容将不会执行此操作.这部分我要留给您,因为我不是在这里为您解决任务:)
The above will not do this. This part I'm going to leave to you because I'm not here to solve the assignment for you :)