r/pythonhelp • • Dec 10 '21

Could someone explain what this code does

(I know that it is a multiplication table I just want an explanation for each code line for an assignment)

startingFactor1Str = input("What is the starting factor1 number? ")
endingFactor1Str = input("What is the ending factor1 number? ")
factor2MaxNumberStr = input(" How far do you want each product to go up for factor2 ? ")
startingFactor1Int = int(startingFactor1Str)
endingFactor1Int = int(endingFactor1Str)
factor2MaxNumberInt = int(factor2MaxNumberStr)
for inputValueXInt in range(startingFactor1Int, endingFactor1Int+1, 1):
for outputValueYInt in range(1, factor2MaxNumberInt+1, 1):
product = inputValueXInt* outputValueYInt
print( str(inputValueXInt) + "*" + str(outputValueYInt) + "=" + str(product))

0 Upvotes

3 comments sorted by

View all comments

3

u/[deleted] Dec 10 '21

Prompt user for some input, and assign the string object returned by input to the variables:

startingFactor1Str = input("What is the starting factor1 number? ")
endingFactor1Str = input("What is the ending factor1 number? ")
factor2MaxNumberStr = input(" How far do you want each product to go up for factor2 ? ")

Because input returns a string object and we need a numeric object, use int() to cast the string to an integer for each of the above and assign to new variables:

startingFactor1Int = int(startingFactor1Str)
endingFactor1Int = int(endingFactor1Str)
factor2MaxNumberInt = int(factor2MaxNumberStr)

create loop using for that counts, using range from the value referenced by the first variable up to and excluding the value calculated by adding one to the value referenced bu the second variable, and count in steps of one. On each iteration of the loop, the count value should be assigned to the variable named just after for. The format of range is range(<start>, <stop>, <step>) where step defaults to one. The count never reaches the stop value but stops one step before it, hence the adding of 1 if you want to reach a particular value.

for inputValueXInt in range(startingFactor1Int, endingFactor1Int+1, 1):

the second for loop is nested within the first, same idea, but this does all its iterations for every iteration of the outer loop

    for outputValueYInt in range(1, factor2MaxNumberInt+1, 1):

so inside the inner loop, calculate the product of the two values referenced by the two variables that are the loop variables from the inner and outerloops (the counters)

        product = inputValueXInt* outputValueYInt

and print out all of the values, converting each numeric value referenced by the variables to strings before concatonating the strings, including the additional bits in double quotes, together. Using f-strings would be better.

        print( str(inputValueXInt) + "*" + str(outputValueYInt) + "=" + str(product))

The variable names are non-standard and a bit too long to be comfortable. Following PEP8 guidance, we usually use all lowercase with _ between words.

Note. In Python, variables do not hold values but instead memory references to Python objects, i.e. where the data of some specific Python object is stored in computer memory.

1

u/[deleted] Dec 11 '21

/u/Y0SH1LOL, did that help you? I think you might have asked a follow-up question, but I can't see it now.

1

u/Y0SH1LOL Dec 11 '21

No that helped a lot thanks