Turbo
Turbo

Reputation: 323

Adding commas in inputs in Python?

I want to make this program in Python 3.3.2 which asks you for the radius and height of a cylinder which you have to type in as radius, height Here is the current code:

if response=="vol":
    radius = float(input("What is the radius and height of the cylinder? (e.g. 32, 15): "))

How should I change it? After that I will calculate its volume: V = hπr2 How will I do this with your method?

Upvotes: 0

Views: 966

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308138

radius, height = [float(part) for part in input("What is the radius and height of the cylinder? (e.g. 32, 15): ").split(',')]

The split splits the input at the comma into two parts; the for is part of a list comprehension, which creates a list by applying the same action to each result.

Upvotes: 2

Related Questions