OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I rewrite this tiny little Python program to make it handle different input data types better?

  • Thread starter Thread starter Rico
  • Start date Start date
R

Rico

Guest
Theoretically I can have two different input data types: str and int. At the moment I'm converting str to int if necessary. But is there maybe another, better solution?

I already looked for some kind of automatic conversion but didn't find anything.

Code:
prompt = "How old are you? Please enter: "

age = input(prompt)

while age != 'quit' and age != 'q':
    age = int(age)
    if age < 3:
        ticket_price = 0
    elif age >= 3 and age < 12:
        ticket_price = 10
    elif age >= 12:
        ticket_price = 15
        
    print(f"The price of your tickets is {ticket_price}")
    print("\nPress 'q' to quit")
    age = input(prompt)
    continue
<p>Theoretically I can have two different input data types: <code>str</code> and <code>int</code>. At the moment I'm converting <code>str</code> to <code>int</code> if necessary. But is there maybe another, better solution?</p>
<p>I already looked for some kind of automatic conversion but didn't find anything.</p>
<pre><code>prompt = "How old are you? Please enter: "

age = input(prompt)

while age != 'quit' and age != 'q':
age = int(age)
if age < 3:
ticket_price = 0
elif age >= 3 and age < 12:
ticket_price = 10
elif age >= 12:
ticket_price = 15

print(f"The price of your tickets is {ticket_price}")
print("\nPress 'q' to quit")
age = input(prompt)
continue
</code></pre>
 

Latest posts

A
Replies
0
Views
1
AgencyAnalytics
A
S
Replies
0
Views
1
Stacker Media
S
C
Replies
0
Views
1
CC.Talent
C
Top