Python Forum
print doesnt work in a function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: print doesnt work in a function (/thread-41739.html)



print doesnt work in a function - ony - Mar-10-2024

Ive been trying to make a random number generator with some luck stuff but im stuck here

import random

randomnumber = random.sample(range(1, 1000), 1)
print(randomnumber)


def luckfactorfunction():
    if randomnumber != 777:

        print("jackpot")
    else:
        if randomnumber != range(1, 776) and range(778, 1000):
            print("better luck next time")

    luckfactorfunction()
print doesnt work even though i cant find any errors


RE: print doesnt work in a function - deanhystad - Mar-10-2024

Your code never calls luckfactorfunction(). Check indenting, I think you have an error with that.

I'm pretty sure this does not do what you think it does:
randomnumber != range(1, 776) and range(778, 1000)
A number will never equal an iterator, so randomnumber != range(1, 776) is always true.

Do not use randome.sample to get a random int in some range. Instead of this:
randomnumber = random.sample(range(1, 1000), 1)
do this:
randomnumber = random.randint(1, 999)
Can you describe what this code is supposed to do?
def luckfactorfunction():
    if randomnumber != 777:
 
        print("jackpot")
    else:
        if randomnumber != range(1, 776) and range(778, 1000):
            print("better luck next time")
 
    luckfactorfunction()



RE: print doesnt work in a function - Pedroski55 - Mar-11-2024

8 is a lucky number in China.

I think you should have at least secondary and tertiary prizes.

Below, the function gives preference to 7 over 8. How will you deal with a number containing 7 and 8?

How will you deal with 778 or 887 or combinations thereof?

import random
 
def luckfactorfunction():
    randomnumber = random.randint(1, 1000)
    if randomnumber == 777: 
        return "You hit the jackpot with 777"
    elif randomnumber == 888: 
        return "You hit the Chinese jackpot with 888"
    elif '77' in str(randomnumber):
        return 'Congrats, you got 2 sevens right!'
    elif '88' in str(randomnumber):
        return 'Congrats, you got 2 Chinese lucky eights right!' 
    elif '7' in str(randomnumber):
        return 'Congrats, you got 1 seven right!'
    elif '8' in str(randomnumber):
        return 'Congrats, you got 1 Chinese lucky eight right!'
    else:
        return "Sorry, nothing, better luck next time." 

for i in range(20):
    luckfactorfunction()