Python Forum
Some line code explanation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Some line code explanation
#1
Hello I am new to python and I still get confused in some parts. I have this code that I want to understand. I can understand most of it but I get confused on lines 6 up to 11.
winningSet =  (10, 11, 8, 1, 5, 20)

numberInput=tuple(input().split())
d = {}
winningPrize = 0
f = 1

if(len(numberInput) == 7):
    for i in range(1,7):
        if int(numberInput[i]) in d:
            f = 0
            break
        else:
            d[int(numberInput[i])] = 1
        if(int(numberInput[i]) in winningSet):
            winningPrize += 100
    if f:
        if winningPrize != 0:
            print(numberInput[0],"won", winningPrize ,"pesos!")
        else:
            print(numberInput[0],"won nothing!")
    else:
        print("No Duplicates")
else:
    print("Should be 6 numbers") 
Reply
#2
You are right, the program is confusing and contains a lot of errors in the logic. For example: "f" as the name of a variable does not make clear what it means. I would have chosen "duplicates" and it should be a boolean. It could contain "True" or "False". It works because "f = 0" also has the meaning of "f = False".
I tried to add some comments. I hope this helps you understand what is going on.
# winningSet is the set of winning numbers.
winningSet = (10, 11, 8, 1, 5, 20)

# numberInput is a tuple of strings, containing 7 chosen numbers, separated by spaces.
numberInput=tuple(input().split())
# It would be better to give some information about the desired input. Like this.
# numberInput = tuple(input("Enter 7 different numbers with spaces inbetween: ").split())

# d is a dictionary to check for double entered numbers.
# If user chose 3, then d[3] will be set to 1.
d = {}

# winningPrize is the amount won; 100 pesos for each correct number.
winningPrize = 0

# f can be 0 or 1. 0 means: duplicates found; 1 means no duplicates found.
f = 1

# There must be 7 items entered. (I think he means 6, the first number is never used.)
# If filled correctly, there will be numberInput[0] through numberInput[6], so 7 items.
if (len(numberInput) == 7):
    # range(1,7) means 1 through 6, so numberInput[0] will be ignored.
    for i in range(1, 7):
        # The first time d will be empty, so go to the "else".
        if int(numberInput[i]) in d:
            # User entered the same number again! So toggle f and exit the for loop.
            f = 0
            break
        else:
            # Mark this number as being used.
            d[int(numberInput[i])] = 1
        # If the number is in the winning set, increase the amount.
        if (int(numberInput[i]) in winningSet):
            winningPrize += 100
    # If f shows there are no duplicates ...
    if f:
        # ... and a prize is won ...
        if winningPrize != 0:
            # ... show the amount that is won.
            # But "numberInput[0] makes no sense, it is the number that is ignored.
            print(numberInput[0], "won", winningPrize, "pesos!")
        # Else no nothing was won.
        else:
            print(numberInput[0], "won nothing!")
    # Else there are duplicates.
    else:
        # This message is not correct.
        print("No Duplicates")
# Else there are not 7 items in numberInput.
else:
    # So this message is not correct.
    print("Should be 6 numbers")
Chrilo06 and BashBedlam like this post
Reply
#3
(Feb-24-2022, 11:16 AM)ibreeden Wrote: You are right, the program is confusing and contains a lot of errors in the logic. For example: "f" as the name of a variable does not make clear what it means. I would have chosen "duplicates" and it should be a boolean. It could contain "True" or "False". It works because "f = 0" also has the meaning of "f = False".
I tried to add some comments. I hope this helps you understand what is going on.
# winningSet is the set of winning numbers.
winningSet = (10, 11, 8, 1, 5, 20)

# numberInput is a tuple of strings, containing 7 chosen numbers, separated by spaces.
numberInput=tuple(input().split())
# It would be better to give some information about the desired input. Like this.
# numberInput = tuple(input("Enter 7 different numbers with spaces inbetween: ").split())

# d is a dictionary to check for double entered numbers.
# If user chose 3, then d[3] will be set to 1.
d = {}

# winningPrize is the amount won; 100 pesos for each correct number.
winningPrize = 0

# f can be 0 or 1. 0 means: duplicates found; 1 means no duplicates found.
f = 1

# There must be 7 items entered. (I think he means 6, the first number is never used.)
# If filled correctly, there will be numberInput[0] through numberInput[6], so 7 items.
if (len(numberInput) == 7):
    # range(1,7) means 1 through 6, so numberInput[0] will be ignored.
    for i in range(1, 7):
        # The first time d will be empty, so go to the "else".
        if int(numberInput[i]) in d:
            # User entered the same number again! So toggle f and exit the for loop.
            f = 0
            break
        else:
            # Mark this number as being used.
            d[int(numberInput[i])] = 1
        # If the number is in the winning set, increase the amount.
        if (int(numberInput[i]) in winningSet):
            winningPrize += 100
    # If f shows there are no duplicates ...
    if f:
        # ... and a prize is won ...
        if winningPrize != 0:
            # ... show the amount that is won.
            # But "numberInput[0] makes no sense, it is the number that is ignored.
            print(numberInput[0], "won", winningPrize, "pesos!")
        # Else no nothing was won.
        else:
            print(numberInput[0], "won nothing!")
    # Else there are duplicates.
    else:
        # This message is not correct.
        print("No Duplicates")
# Else there are not 7 items in numberInput.
else:
    # So this message is not correct.
    print("Should be 6 numbers")
Thank you so much! this means a lot to me since I always get confused from time to time in python. Forgot to mention the numberinput[0] is supposed to be the name of the user
Reply
#4
numberInput[0] was probably supposed to be some kind of ID, maybe even a name. I think the purpose of the code is to enter some form of ID and 6 numbers.

I would put the "verify input" in a function. This code loops until the user enters valid input.
winning_numbers =  {10, 11, 8, 1, 5, 20}

def get_info():
    """Input name and 6 unique lottery number choices"""
    name = input("Enter you name: ")
    while True:
        numbers = input("Enter your 6 lottery numbers separated by spaces: ").split()
        if len(numbers) != 6:
            print("Should be 6 Numbers")
            continue

        if len(set(numbers)) != 6:
            print("There can be no duplicates")
            continue

        # Convert the numbers to integers
        try:
            numbers = list(map(int, numbers))
        except ValueError:
            print("Numbers must be integers")
            continue

        return name, numbers

name, entered_numbers = get_info()
prize = len(winning_numbers & set(entered_numbers)) * 100
if prize > 0:
    print(name, "won", prize ,"pesos!")
else:
    print(name, "won nothing!")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New learner in Python, and need help on the term explanation BaicaiPy 3 1,291 Oct-15-2022, 03:31 PM
Last Post: Yoriz
  XOR solution explanation needed. omm 7 3,195 Oct-26-2020, 06:30 AM
Last Post: omm
  While statement explanation alkhufu2 3 2,393 Sep-02-2020, 05:46 PM
Last Post: alkhufu2
  How to repeat input line of code until condition is met Reta 2 3,381 May-14-2019, 10:06 PM
Last Post: nilamo
  Python code unable to show Bokeh line chart kirito85 2 2,523 Feb-06-2019, 07:52 AM
Last Post: kirito85
  Output explanation AmanTripathi 2 2,803 Feb-14-2018, 03:03 PM
Last Post: AmanTripathi
  need help with some explanation vincelim99 2 3,644 Mar-24-2017, 04:12 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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