Python Forum
Some line code explanation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Some line code explanation (/thread-36480.html)



Some line code explanation - Chrilo06 - Feb-24-2022

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") 



RE: Some line code explanation - ibreeden - Feb-24-2022

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")



RE: Some line code explanation - Chrilo06 - Feb-24-2022

(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


RE: Some line code explanation - deanhystad - Feb-24-2022

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!")