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 Scripts
CookBook - Shmup - PyQt5 Music Player


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)')
menator01 and naughtyCat like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 170 Oct-06-2021, 09:39 AM
Last Post: menator01
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 257 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 433 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  I have a syntax error! rohes_kaugummi 3 625 Jun-23-2021, 05:24 PM
Last Post: deanhystad
  import module with syntax error Skaperen 7 728 Jun-22-2021, 10:38 AM
Last Post: Skaperen
Heart near "(": syntax error Maryan 3 864 Jun-21-2021, 08:05 AM
Last Post: ibreeden
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 2,440 Jun-15-2021, 08:16 AM
Last Post: Anldra12
Exclamation getting a syntax error and i don't know why ilikedofs 10 1,229 May-13-2021, 08:52 PM
Last Post: deanhystad
  Even number code syntax error MrCeez 1 640 May-02-2021, 06:43 PM
Last Post: Larz60+
  error in your SQL syntax Xuli 10 1,138 May-02-2021, 12:27 PM
Last Post: Xuli

Forum Jump:

User Panel Messages

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