1

The given question is depending upon the number of inputs given (1 input, 2inputs, or 3) finding the circumference of the circle and perimeter's of rectangle and triangle. I can't seem to figure out how to read the number of inputs give ex: if I give only one input ex:2 the output should be "12.56" and if I give 2 inputs say 2 4 output should be "6"(2*(a+b)) so far I'm done till functions and I'm stuck at inputs

def cir(a):
    x=2*3.142*a
    return x
def rec(a,b):
    y=2*(int(a+b))
    return y
def tri(a,b,c):
    z=a+b+c
    return z
a=b=c=0
print("enter the dimenssions")
a,b,c=float(input()),float(input()),float(input())
3
  • How are you wanting to separate the inputs? With a space? Commented Oct 28, 2021 at 14:16
  • 1
    See tutorial on taking multiple inputs in Python here. Commented Oct 28, 2021 at 14:19
  • @doctorlove yes I want to separate the inputs with space Commented Oct 28, 2021 at 14:39

4 Answers 4

2

If I understand correctly this should work (Presuming that you want to separate on a space)

def cir(a):
    x=2.0*3.142*float(a)
    return x
def rec(a,b):
    y=2*(float(a)+float(b))
    
    return y
def tri(a,b,c):
    z=float(a)+float(b)+float(c)
    return z

#fun fact, you can put a string inside "input()" and it will have that right before you can type
userInput = input("Enter dimenssions: ")
userInput = userInput.split(" ")#Split the input on all  the spaces

numInputs = len(userInput)#Get how many numbers there are

if(numInputs == 1):
    print("Calculating for cir")
    print(cir(userInput[0]))
elif(numInputs == 2):
    print("Calculating for rect")
    print(rec(userInput[0], userInput[1]))
elif(numInputs == 3):
    print("Calculating for tri")
    print(tri(userInput[0], userInput[1], userInput[2]))
else:
    print("Error, you entered something wrong :(")

Tests:

Enter dimenssions: 5
Calculating for cir
31.419999999999998
Enter dimenssions: 15 7
Calculating for rect
44.0
Enter dimenssions: 10 2 7
Calculating for tri
19.0

I did have to change your perimeter functions for the way I have implemented this.

Sign up to request clarification or add additional context in comments.

Comments

2

Use str.split:

def cir(a):
    return 2 * 3.142 * a
def rec(a, b):
    return 2 * (a + b)
def tri(a, b, c):
    return a + b + c

funcs = {1: cir, 2: rec, 3: tri}

try:
    inputs = list(map(float, input("Please give floats space separated ").split()))
except ValueError:
    print("Values must be floats")

try:
    func = funcs[len(inputs)]
    print(f"Using {func.__name__}")
    print(func(*inputs))
except IndexError:
    print("Too many inputs given must be 3 or less")

Please give floats space separated 1
Using cir
6.284

Please give floats space separated 1 2
Using rec
6.0

Please give floats space separated 1 2 3
Using tri
6.0

Comments

1

Here we are

def cir(a):
    x=2*3.142*a
    return x
def rec(a,b):
    y=2*(int(a+b))
    return y
def tri(a,b,c):
    z=a+b+c
    return z

l_input = [float(x) for x in input("Input values:").split()]

print("Input = {}".format(l_input))

if len(l_input) == 1:
    print(cir(*l_input))
elif len(l_input) == 2:
    print(rec(*l_input))
elif len(l_input) == 3:
    print(tri(*l_input))

Sample output:

Input values:1
Input = [1.0]
6.284


Input values: 1 2
Input = [1.0, 2.0]
6

Input values: 1 2 3 
Input = [1.0, 2.0, 3.0]
6.0

Comments

1

maybe instead "a,b,c=float(input()),float(input()),float(input())" something like this:

param = input().split()
if len(param) == 1:
    print(cir(float(param[0])))
elif len(param) == 2:
    print(rec(float(param[0]), float(param[1])))
elif len(param) == 3:
    print(tri(float(param[0]), float(param[1]), float(param[2])))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.