New to python looking for help

Joined
Sep 26, 2023
Messages
2
Reaction score
0
Hi I have been studying python for a few weeks and have made a few small projects, one of them being a lottery number generator. I was wondering what typical nooby mistakes I have made, also what I could do to clean up my code to make it look nicer. Would also help if someone would give me a suggestion on what else I could add onto it (don't tell me how to do it just give me suggestion please)

import random
nums = list(range(1,51))
nums2 = list(range(1,12))
random.shuffle(nums)
random.shuffle(nums2)
range_of_numbers = nums[:5]
range_of_numbers2 = nums2[:2]
range_of_numbers2.sort()
range_of_numbers.sort()

print("Your Lottery Numbers Are: " + str(range_of_numbers) + ("\nYour Jackpot Numbers Are: ") + str(range_of_numbers2))
question1 = input("Are you happy with your Numbers?")
print("Good Luck! ") if question1 == ("Yes") or ("yes") else print ("Sorry try again")

while question1 == ("No"):
print("Your Lottery Numbers Are: " + str(range_of_numbers) + ("\nYour Jackpot Numbers Are: ") + str(range_of_numbers2))
question1 = input("Are you happy with your Numbers?: ")

Also if anyone has any courses i can take to better my skills id be more than happy to hear it , im currently doing the cs50p course.
Thanks.
 
Joined
Dec 10, 2022
Messages
73
Reaction score
22
I would probably take this approach. Note: There is not any error checking in this example.

PHP:
from random import choices

# Create a list of numbers
numbers = list(range(1, 101))

# Start the while loop
while True:

    # Create a lottery number of 7 numbers
    random_numbers = choices(numbers, k=7)

    # Print instructions
    print('Enter 7 numbers between 1 and 100 seperated by a space')

    # Get user input and put in a list
    guess = input('>> ').split()

    # Sort the numbers
    numbers.sort()
    guess.sort()
    
    # Compare the list
    if guess == random_numbers:
        print('You win the lottery')
    else:
        print('Sorry, You didn\'t win.')
    
    # Ask if user wants to play again
    ask = input('Play Again? (y/n)\>> ')
    if ask.lower() == 'n':
        print('Goodbye!')
        break
 
Joined
Sep 26, 2023
Messages
2
Reaction score
0
I would probably take this approach. Note: There is not any error checking in this example.

PHP:
from random import choices

# Create a list of numbers
numbers = list(range(1, 101))

# Start the while loop
while True:

    # Create a lottery number of 7 numbers
    random_numbers = choices(numbers, k=7)

    # Print instructions
    print('Enter 7 numbers between 1 and 100 seperated by a space')

    # Get user input and put in a list
    guess = input('>> ').split()

    # Sort the numbers
    numbers.sort()
    guess.sort()
   
    # Compare the list
    if guess == random_numbers:
        print('You win the lottery')
    else:
        print('Sorry, You didn\'t win.')
   
    # Ask if user wants to play again
    ask = input('Play Again? (y/n)\>> ')
    if ask.lower() == 'n':
        print('Goodbye!')
        break
Thanks for this, its really helpful to look at code created by someone more experienced to understand the structure of how code should look. Do you know any courses I could take that you could recommend that would help me with my journey to learning python? I'm looking for something with a lot of practical work (building projects ect) as appose to just theory work and watching videos. Thanks again!!
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Python:
...

    random_numbers = choices(numbers, k=7)
    guess = input('>> ').split()
    ...
 
    # Compare the list
    if guess == random_numbers:
        print('You win the lottery')
    else:
        print('Sorry, You didn\'t win.')
Seriously? ;)

The probability of hitting a win is approximately 7! * 100!
Good luck with this game :cool:
[ on-line ]
Python:
from random import choices
from time import sleep

# Create a list of numbers
numbers = list(range(1, 101))

# Create a lottery number of 7 numbers
random_numbers = choices(numbers, k=7)

# Sort the numbers
random_numbers.sort()

guess_counter = 0

# Start the while loop
while True:
    guess = choices(numbers, k=7)  # Simulation of user input seven numbers
    guess.sort()
   
    guess_counter = guess_counter + 1
    print(f'-{guess_counter:-<28}')
    print(f'{random_numbers}\n{guess}\n', sep='')
   
    sleep(.5)
   
    if guess == random_numbers:
        break

print(f'You won after {guess_counter} draws')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top