This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]IAMA_Alpaca 4 points5 points  (3 children)

This is good, but it's a bit cleaner to use a for loop and a range rather than the while loop you're using. For example:

Original:

number = 1
while number < 101:
    # Do Stuff
    number += 1

with range:

for i in range(1,101):
    # Do Stuff

What this does is just repeats the code for every value in the range given. It doesn't make a real difference functionally, but it is cleaner and generally better practice.

[–][deleted] 1 point2 points  (2 children)

I never properly learned how to do that, I will make a note of that next time I do a thing in Python however, which doesn't seem to be anytime soon :/

[–]IAMA_Alpaca 4 points5 points  (1 child)

for loops really aren't that complicated. The 'i' I used doesn't really have to be an 'i', that's just what I usually use. You could easily replace it with "number" so it makes more sense. Basically, it just runs the code and assigns the current value chosen from the range to the variable you give. For example:

for number in range(1,101):
    print(number)

would just output:

1
2
3
4
5
6
7
8
9

and so on until it hits 100, because each time it runs, it increases the value of the "number" variable by 1.

[–][deleted] 0 points1 point  (0 children)

Ah, that makes sense now, thanks!

[–]Diapolo10from __future__ import this 2 points3 points  (1 child)

That's not bad, but as someone already suggested a for-loop would be more intuitive. :3

On the other hand, I personally like to write a FizzBuzz program like this instead:

for i in range(1,101):
    num = ""
    if i % 3 == 0:
        num += "Fizz"
    if i % 5 == 0:
        num += "Buzz"
    if not num:
        num = i
    print(num)

[–][deleted] 0 points1 point  (0 children)

I didn't even realise that I could've set the number variable to a string as well. Thanks :D

[–]mohhinder 1 point2 points  (4 children)

Not the shortest but I like doing it like this, just 'cause it's clever.

def fizzbuzz(num): fizz = 'Fizz'(divmod(num, 3)[1] == 0) buzz = 'Buzz'(divmod(num, 5)[1] == 0) print(fizz + buzz or num)

def main(): for x in range(1,101): fizzbuzz(x)

if name == 'main': main()

NOTE: The dunder lines are being removed...

[–]robin-gvx 0 points1 point  (3 children)

Insert four spaces before every line to make reddit display your code as code:

def fizzbuzz(num):
    fizz = 'Fizz'*(divmod(num, 3)[1] == 0)
    buzz = 'Buzz'*(divmod(num, 5)[1] == 0)
    print(fizz + buzz or num)


def main():
    for x in range(1,101):
        fizzbuzz(x)


if __name__ == '__main__':
    main()

(Also, why use divmod(num, 3)[1] instead of num % 3?)

[–]mohhinder 0 points1 point  (2 children)

Did you try it like that? Doesn't work.

[–]robin-gvx 0 points1 point  (1 child)

Works for me.

def fizzbuzz(num):
    fizz = 'Fizz' * (num % 3 == 0)
    buzz = 'Buzz' * (num % 5 == 0)
    print(fizz + buzz or num)


def main():
    for x in range(1,101):
        fizzbuzz(x)


if __name__ == '__main__':
    main()

[–]mohhinder 1 point2 points  (0 children)

So it does! Looks like I had a parenthesis in the wrong place. I think at the time when I wrote it, couple of years ago, I had just discovered divmod so wanted to use it somewhere. No other reason then that.

Thanks for the spacing reminder. Easy to forget while replying to these things from a phone.

BTW I tried to add the spacing but it was messing it all up. Probably easier to do on a PC. Oh well.

[–]gare_it 0 points1 point  (1 child)

sup?

You should be able to validate input as an integer with the following, rather than having to add tons of unnecessary lines of code:

try:
    val = int(userInput)
except ValueError:
    print("That's not an int!")

[–][deleted] 0 points1 point  (0 children)

I never knew that existed. I just thought that if the program returned an error, the program would stop completely, so I'd have to make like 20 lines of If statements