-2

Is it possible to use only part of the input from the user and neglect the parts where there is no input given?

Say I want to calculate the used volume of a cylinder filled with a liquid. I would ask the user for input, for example the volume of the cylinder is given together with the height of the cylinder, than the program should be able to return the diameter of the cylinder, the circumference, the surface area, ...

Say that the user can only provide the diameter and the fill percentage, the program should be able to return the same things as previous.

I tried to ask for input as follows:

volume = float(input("volume in qubic meters: ") or None
diameter = float(input("tankdiameter: ") or None
# etc...

Then I would write a class:

class object:
    __init__(volume, diameter, heigth, fill_percentage, radius, circumference, surface_area,...)

    self.volume = volume
    # etc...

Next there will be functions in the class to calculate the not provided items for instance if height and diameter are given, it calculates the volume. But I'm getting stuck with the input of the user. Eventually it's the idea that there comes a GUI where the input fields are being linked to the code.

Any suggestions?

3
  • What do you hope or None should accomplish? foo = input("type something: ") or "default" assigns "default" if the user doesn't type anything. Commented Nov 14, 2024 at 11:55
  • btw, even though it's an example, never write class object: it will overwrite the python base object class which is the last thing anyone would want :) Commented Nov 14, 2024 at 15:42
  • 1
    Both the expressions float(input("volume in qubic meters: ") or None and float(input("tankdiameter: ") or None are not syntactically valid as they're missing closing parenthesis ). It's not clear if this is where you're "getting stuck with the input of the user" since you haven't said what you exactly mean by "getting stuck" or shared any error messages/stack traces as part of a minimal reproducible example. Commented Nov 14, 2024 at 15:50

1 Answer 1

0

You could try the following:

try:
    a = float(input("a: "))
except ValueError:
    a = None

This sets a to None if the user doesn't enter anything or a string. So, entering true and 'asdf' will not work, but 22, 21.0, 22.3 will work. (Integer values are just converted to the x.0)

You can put this in a function if you like, taking x as a parameter, setting a value of x, and returning it. Depends on your use case.

Hope this helps.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.