Python Forum
Line 42 syntax error..Help!!1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Line 42 syntax error..Help!!1
#4
I liked this. I did a version.

#! /usr/bin/env python3

# Do the imports
import random as rnd

# Get the lottery numbers
lottery_nums = rnd.sample(range(1, 61), 5)

# Sort the lottery numbers for display
lottery_nums.sort()

# Create empty list
user_nums = []

# Message to display in various areas
message = 'Please enter a number between 1 and 60'

print(message)

# Start a while loop. Will end when we have 5 numbers in our list
while len(user_nums) < 5:
    num = input('>> ')
    try:

        # convert input to int
        num = int(num)
    except ValueError:

        # Throw error if not in the correct format
        print(f'{num} is not a number. {message}')
        continue
    # Check that our numbers are withn the correct range
    if num < 1 or num > 60:
        print(f'That number is not in range. {message}.')
        continue
    # Check if the number has already been picked
    if num in user_nums:
        print(f'You have already picked {num}. Please try again.')
        continue
    else:

        # Append the number to user picks
        user_nums.append(num)

# Sort user numbers for display
user_nums.sort()

# Check for matches in the two list
matches = len([key for key, val in enumerate(lottery_nums) if val in set(user_nums)])

# Display information
print(f'Lottery Numbers: {lottery_nums}')
print(f'Player Numbers:  {user_nums}')
print(f'You matched {matches} number(s)')
Output:
Please enter a number between 1 and 60 >> 20 >> 15 >> 15 You have already picked 15. Please try again. >> 33 >> 61 That number is not in range. Please enter a number between 1 and 60. >> 60 >> 14 Lottery Numbers: [26, 43, 44, 51, 60] Player Numbers: [14, 15, 20, 33, 60] You matched 1 number(s)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Line 42 syntax error..Help!!1 - by patpython - Aug-30-2021, 11:12 PM
RE: Line 42 syntax error..Help!!1 - by bowlofred - Aug-30-2021, 11:22 PM
RE: Line 42 syntax error..Help!!1 - by patpython - Aug-30-2021, 11:35 PM
RE: Line 42 syntax error..Help!!1 - by menator01 - Aug-31-2021, 07:08 PM
RE: Line 42 syntax error..Help!!1 - by deanhystad - Sep-01-2021, 03:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  World Clock syntax error OscarBoots 1 143 May-03-2024, 05:20 AM
Last Post: snippsat
  Syntax error for "root = Tk()" dlwaddel 15 1,281 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 431 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,680 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,267 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,659 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  syntax error question - string mgallotti 5 1,357 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,319 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 931 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,905 Sep-17-2022, 04:09 AM
Last Post: jttolleson

Forum Jump:

User Panel Messages

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