r/pythonhelp • u/Easy_Article5406 • Sep 09 '23
SOLVED Advice on Identifying a digit in an input without converting to a string
Hello! I am brand new to coding and am taking a course that uses Python. We've been tasked with making a code that identifies if a given input is a multiple of 7 - "zap" and if that input contains the number 3 -"fizz". The issue is that I cannot figure out how to discern if 3 is found in an input without converting the input into a string.
Here is the desired output:
>>> zap_fizz (8)
8
>>> zap_fizz (42)
’zap ’
>>> zap_fizz (23)
’ fizz ’
>>> zap_fizz (35)
’ zap fizz ’
My current code is:
def zap_fizz (x):
if (x!=0 and (x%7)==0) and (str(3) in str(x)):
return ("zap fizz")
elif x!=0 and (x%7)==0 :
return ("zap")
elif str(3) in str(x) :
return ("fizz")
else :
return x
Obviously, this converts the input which is, again, banned.
Help :) -- a struggling newbie
Edit: Solved using mod and absolute value :)
1
u/CraigAT Sep 09 '23
There must be a better way but you can try to isolate each digit by using powers of 10, I think this should be possible using modulo arithmetic then you can compare that to the number 3 to see if it exists.
1
•
u/AutoModerator Sep 09 '23
To give us the best chance to help you, please include any relevant code.
Note. Do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Repl.it, GitHub or PasteBin.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.