Question
Question: Using Python, The program must perform the following tasks: 1. Display a description of the programs purpose 2. Prompt the user for a 3-digit
Question: Using Python, The program must perform the following tasks: 1. Display a description of the programs purpose 2. Prompt the user for a 3-digit CVN 3. Calculate the sum of the absolute difference between each digit 4. Display the result
What I have:
def main(): # Display a description of the program's purpose print("This program determines the weight of an Integer") print("using the sum of the absolute difference between each digit.") # Prompt the user for a 3-digit CVN x=int(input("Please enter a 3-digit CVN: ")) # Call the weight() function providing the 3-digit # CVN as an argument weight(x) # The weight function requires and Integer parameter def weight(cvn): # Convert the Integer to a String num_str=str(cvn) # Initialize any necessary variables sum=0 y=0 # Setup a for loop that uses the digits as a sequence for i in num_str: # Sum the differences between each digit sum=sum+abs(y-int(i)) y=int(i) # Print the calculated weight of the integer print("The calculated weight of the integer is ", sum) # Call the main() function main()
Issue: math is incorrect
for example, when i enter 783 I get 3 for the weight and not 13
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started