Python FizzBu​​zz [英] Python FizzBuzz

查看:91
本文介绍了Python FizzBu​​zz的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经给我这个问题要在Python中完成:

I have been given this question to do in Python:

从用户那里获取一个数字列表,然后在该列表上运行FizzBu​​zz.

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整除,则打印"FizzBu​​zz"

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 :)

这篇关于Python FizzBu​​zz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆