-
Notifications
You must be signed in to change notification settings - Fork 12.7k
code - Sum of digits of a number #1969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
geekcomputers
merged 1 commit into
geekcomputers:master
from
sarayusreeyadavpadala:code
Aug 28, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,33 @@ | ||
q=0 # Initially we assigned 0 to "q", to use this variable for the summation purpose below. | ||
# The "q" value should be declared before using it(mandatory). And this value can be changed later. | ||
# Python code to calculate the sum of digits of a number, by taking number input from user. | ||
|
||
n=int(input("Enter Number: ")) # asking user for input | ||
while n>0: # Until "n" is greater than 0, execute the loop. This means that until all the digits of "n" got extracted. | ||
import sys | ||
|
||
r=n%10 # Here, we are extracting each digit from "n" starting from one's place to ten's and hundred's... so on. | ||
def get_integer(): | ||
for i in range(3,0,-1): # executes the loop 3 times. Giving 3 chances to the user. | ||
num = input("enter a number:") | ||
if num.isnumeric(): # checks if entered input is an integer string or not. | ||
num = int(num) # converting integer string to integer. And returns it to where function is called. | ||
return num | ||
else: | ||
print("enter integer only") | ||
print(f'{i-1} chances are left' if (i-1)>1 else f'{i-1} chance is left') # prints if user entered wrong input and chances left. | ||
continue | ||
|
||
|
||
q=q+r # Each extracted number is being added to "q". | ||
def addition(num): | ||
Sum=0 | ||
if type(num) is type(None): # Checks if number type is none or not. If type is none program exits. | ||
print("Try again!") | ||
sys.exit() | ||
while num > 0: # Addition- adding the digits in the number. | ||
digit = int(num % 10) | ||
Sum += digit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also use doc-string on the main function. |
||
num /= 10 | ||
return Sum # Returns sum to where the function is called. | ||
|
||
n=n//10 # "n" value is being changed in every iteration. Dividing with 10 gives exact digits in that number, reducing one digit in every iteration from one's place. | ||
|
||
print("Sum of digits is: "+str(q)) | ||
|
||
if __name__ == '__main__': # this is used to overcome the problems while importing this file. | ||
number = get_integer() | ||
Sum = addition(number) | ||
print(f'Sum of digits of {number} is {Sum}') # Prints the sum |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print(f"{i-1} {'chance' if i-1 == 1 else 'chances'} left")
This is better readability I think.
But, your is fine, too!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @NitkarshChourasia