Line 42 syntax error..Help!!1 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Line 42 syntax error..Help!!1 (/thread-34772.html) |
Line 42 syntax error..Help!!1 - patpython - Aug-30-2021 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.") RE: Line 42 syntax error..Help!!1 - bowlofred - Aug-30-2021 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).
RE: Line 42 syntax error..Help!!1 - patpython - Aug-30-2021 Thanks for your help. The code is fixed. RE: Line 42 syntax error..Help!!1 - menator01 - Aug-31-2021 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)')
RE: Line 42 syntax error..Help!!1 - deanhystad - Sep-01-2021 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)') |