Python Forum
print doesnt work in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print doesnt work in a function
#1
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
Reply
#2
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()
Reply
#3
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pydoc documentation doesnt work Cosmosso 5 4,395 Nov-25-2023, 11:17 PM
Last Post: vidito
  pip install requests doesnt work misodca 8 6,181 Jul-07-2023, 08:04 AM
Last Post: zyple
  How to print variables in function? samuelbachorik 3 916 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Ldap3 Python print(conn.entries) doesnt work ilknurg 15 5,789 Dec-28-2022, 11:22 AM
Last Post: shad
  I dont know why my function won't work? MehHz2526 3 1,206 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  pip install pystyle doesnt work person_probably 2 2,124 Sep-23-2022, 02:59 PM
Last Post: person_probably
  How to print the output of a defined function bshoushtarian 4 1,318 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Why doesnt chunk generation work? LotosProgramer 1 1,953 Apr-02-2022, 08:25 AM
Last Post: deanhystad
  Why does absence of print command outputs quotes in function? Mark17 2 1,392 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  return vs. print in nested function example Mark17 4 1,757 Jan-04-2022, 06:02 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020