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
#1
Hello, I'm just getting my feet wet in python, and I can't seem to figure out line 42 syntax error. Can someone give me some wisdom on how to fix this please. It's the last line. The syntax error symbol is under the word "correctly". Here is the code

#Lottery Numbers - www.101computing.net/lottery-numbers

import random

#Initialise an empty list that will be used to store the 5 lucky numbers!
lotteryNumbers = []

for i in range (0,5):
  number = random.randint(1,60)
  #Check if this number has already been picked and ...
  while number in lotteryNumbers:
    # ... if it has, pick a new number instead 
    number = random.randint(1,60)

  #Now that we have a unique number, let's append it to our list.
  lotteryNumbers.append(number)

#Sort the list in ascending order
lotteryNumbers.sort()

userNumbers = []
for i in range(0,5):
   number = int(input("Please enter a number between 1 and 60:"))
while (number in userNumbers or number<1 or number>60):
  print("Invalid number, please try again.")
  number = int(input("Please enter a number between 1 and 60:"))

userNumbers.append(number)

#Display the list on screen:
print(">>> Today's lottery numbers are: ")
print(lotteryNumbes)

print(">>> Your selection:")
print(userNumbes)

counter = 0
for number in userNumbers:
  if number in lotteryNumbers:
    counter +=1

print("You guessed " + str(counter) + - number(s) correctly.")
Yoriz write Aug-31-2021, 05:01 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
The final line doesn't have a balanced number of quotation marks. The string after counter needs to be in quotes. This would be a little bit easier to do with f-strings. Consider instead:

print(f"You guessed {counter} number(s) correctly.")
Also you are using a random function that picks with the possibility of duplicates and then doing logic to avoid duplicates. You could instead use the method that picks without duplicates

no_duplicates = random.sample(range(1,60), 5)
You are using range(1,60) for the possible picks. That does not include the number 60. If you want 60 to be possible, you need range(1,61).
patpython likes this post
Reply
#3
Thanks for your help. The code is fixed.
Reply
#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
#5
If you have two sets of numbers you can use & to get the intersection (numbers in both sets).
import random

def input_unique_int(min_, max_, used_values =None):
    '''Input a number in range min_ to max_.  used_values is a list of disallowed numbers'''
    while True:
        value = input(f'Enter a number in the range {min_} and {max_} ')
        try:
            value = int(value)
        except ValueError:  # Input is not an integer
            print('Must be an integer number')
            continue
        if value < min_ or value > max_:  # Input is not in range
            print(f'Number must be in range {min_} and {max_}')
            continue
        if used_values is not None and value in used_values :  # Cannot enter values from used_values list
            print(f'That number is already used')
            continue
        return value

winner = random.sample(range(1, 61), 5)
ticket = []
for i in range(len(winner)):
    ticket.append(input_unique_int(1, 60, ticket))

print(f'Winning Numbers: {winner}')
print(f'Your Numbers:  {ticket}')
matches = len(set(winner) & set(ticket))
if matches == len(winner):
    print('You won the lottery!!!!!')
else:
    print(f'You matched {matches} number(s)')
naughtyCat and menator01 like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,160 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 374 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,545 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,206 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,545 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  syntax error question - string mgallotti 5 1,297 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,243 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 882 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,829 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,360 Jun-18-2022, 01:09 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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