Python Forum
Some line code explanation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Some line code explanation
#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


Messages In This Thread
Some line code explanation - by Chrilo06 - Feb-24-2022, 03:57 AM
RE: Some line code explanation - by ibreeden - Feb-24-2022, 11:16 AM
RE: Some line code explanation - by Chrilo06 - Feb-24-2022, 02:53 PM
RE: Some line code explanation - by deanhystad - Feb-24-2022, 06:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  New learner in Python, and need help on the term explanation BaicaiPy 3 1,378 Oct-15-2022, 03:31 PM
Last Post: Yoriz
  XOR solution explanation needed. omm 7 3,370 Oct-26-2020, 06:30 AM
Last Post: omm
  While statement explanation alkhufu2 3 2,466 Sep-02-2020, 05:46 PM
Last Post: alkhufu2
  How to repeat input line of code until condition is met Reta 2 3,455 May-14-2019, 10:06 PM
Last Post: nilamo
  Python code unable to show Bokeh line chart kirito85 2 2,579 Feb-06-2019, 07:52 AM
Last Post: kirito85
  Output explanation AmanTripathi 2 2,914 Feb-14-2018, 03:03 PM
Last Post: AmanTripathi
  need help with some explanation vincelim99 2 3,704 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