Reputation: 31
I would like to convert the input into a range with interval. Can anyone please help me.
Ask the user what will be the input of the user either in Celsius or Fahrenheit. If the input is invalid, print invalid and ask to try again.
Ask the user to input the start, range, and interval separated by an asterisk. For example 0*10*2. This means that the start of the conversion will be from 0 to 10 with an interval of 2, thus the value to be converted will be 0, 3, 6, 9
If the start<end, then the interval should be 0<interval otherwise, your program will display an error and ask to try again for #2. Or
If the start>end, then the interval should be 0>interval otherwise, your program will display an error and ask to try again for #2.
If the user inputs only one value, like 10. This means that the start is equal to 1 which will be the default value for start, up to 10, and the interval is 2 which will be the default value of the interval if start<end.
If the user input has two values like 10*2, this means that the start is equal to 10 up to 2. The interval is set to the default value which is equal to -2 since start>end.
ask = input("What would you like to choose? Celcius = 1, Fahrenheit = 2. \nPick option 1 or 2: ")
asks = float(ask)
if asks == 1:
rng = input("Enter the start, range, interval. \n example 0*10*2:").split("*")
rng = range(rng)
print(rng)
Upvotes: 2
Views: 639
Reputation: 2402
import sys
def get_celsisu_or_fahrenheit():
print("What would you like to choose: \n1. Celsius\n2. Fahrenheit\n")
cf = input("Enter (1/2): ")
if cf not in ["1", "2"]:
err()
get_celsisu_or_fahrenheit()
return cf
def get_value(is_celsuis):
values = input(f"Enter the value in {'Celsius' if is_celsuis else 'Fahrenheit'}: ").split("*")
values = list(map(lambda x: int(x), values))
start, end, interval = assign_values(values)
# conditions
if start < end and not 0 < interval:
err() and sys.exit(0)
if start > end and not 0 > interval:
err() and sys.exit(0)
return start, end, interval
def assign_values(val):
start, end, interval = 1, None, None
if len(val) == 1:
end = val[0]
elif len(val) == 2:
start, end = val
elif len(val) == 3:
start, end, interval = val
else:
err()
get_value()
if interval is None:
interval = -2 if start > end else 2
return start, end, interval
def err():
print("ERROR: Please check your input and try again.")
print("----------------------------------------------")
def convert(grade, is_celsuis):
return (grade * 9 / 5) + 32 if is_celsuis else (grade - 32) * 5 / 9
def start():
is_celsuis = True if get_celsisu_or_fahrenheit() == "1" else False
start, end, interval = get_value(is_celsuis)
rng = range(start, end, interval)
print(rng)
for x in rng:
is_celsuis and print(f"{x} Celsuis == {round(convert(x, is_celsuis), 2)} Fahrenheit")
not is_celsuis and print(f"{x} Fahrenheit == {round(convert(x, is_celsuis), 2)} Celsuis")
start()
Upvotes: 1
Reputation: 806
answer1_flag = 0
while answer1_flag == 0:
ask = input("What would you like to choose? Celcius = 1, Fahrenheit = 2. \nPick option 1 or 2: ").strip()
if ask == '1' or ask=='2':
answer1_flag = 1
answer2_flag = 0
while answer2_flag==0:
rng = input("Enter the start, range, interval. \n example 0*10*2:").strip()
count = rng.count('*')
if count==0:
start = 1
end = int(rng)
interval = 2
elif count==1:
start,end = map(int,rng.split("*"))
if start<end:
interval = 2
elif start>end:
interval = -2
elif count==2:
start,end,interval = map(int,rng.split("*"))
if start>end and interval>0:
print('Error! If start>end then interval should be a negative number! Please enter the input again.')
elif start<end and interval<0:
print('Error! If start<end then interval should be a positive number! Please enter the input again.')
else:
rng = range(start,end,interval)
answer2_flag = 1
else:
print('Invalid! Try again.')
print(rng)
Upvotes: 1