Home > Blockchain >  Avoiding having to write several print calls python
Avoiding having to write several print calls python

Time:09-17

I wrote a really short program but I hate having so many repetitive print calls that makes it seem as if a two year old coded my program.

userInput %= 6  
print (userInput)
userInput  = userInput 
print (userInput)
userInput -= 4 
print (userInput)
userInput  = 1 
print (userInput)
userInput *= 9.857143 
print (userInput)
userInput = str(userInput)
print ("<"   userInput   ">")

you can see how repetitive and ugly it is, I tried searching up and tried several things but they dont really work with what Im trying to do here.

CodePudding user response:

Could just write a basic function, or if you don't want anyone to see your code, you could create a class and reference it instead. You would just create a new file, paste the class code into it, save it as userinput, then run UserInputter(somevaluehere) in the main code file. Doesn't really get rid of the problem, but prevents others from seeing your print statements unless they look at the class code.

class UserInputter:

    def __init__(self,userInput):
        userInput %= 6  
        print (userInput)
        userInput  = userInput 
        print (userInput)
        userInput -= 4 
        print (userInput)
        userInput  = 1 
        print (userInput)
        userInput *= 9.857143 
        print (userInput)
        userInput = str(userInput)
        print ("<"   userInput   ">")

from userinput import UserInputter
UserInputter(somevalue)

CodePudding user response:

You could try this :

import operator
userinput = int(input("enter the number : "))

for operator, sec_operand in {operator.mod: 6, operator.add: userinput, operator.sub: 4}.items() :
    print(operator(userinput, sec_operand))
  • Related