0

I am currently writing a program that outputs the lowest and highest numbers of an input_list.

currently I am using

list_of_num = [input_list for input_list in input("Input 4 numbers separated by comma's to find the lowest and highest numbers:\n").split(",")]

to separate the numbers input by a user, and then converting to a list to compare the numbers individually.

This is done by using:

maximum_output_number = list_of_num[0]

for y in range(len(list_of_num)):

    # if the other element is max then last element.
    if list_of_num[y] > maximum_output_number:
        maximum_output_number = list_of_num[y]

then simply printing the maximum_output_number.

so E.G. input(20,-40,-10,14.5) would output "the highest number in the list is 20" etc.

However, I want to include the option that if a user inputs the word "generate", the program will use

random.sample(range(-1000000, 1000000), 4)

to generate a list and then use that generated list to output the highest and lowest numbers.

2 Answers 2

1

You need to use if-else to check if the user inputs "generate" or not.

This should work

import random
x = input("Input 4 numbers separated by comma's to find the lowest and highest numbers:\n")

if x != "generate":  #If x is not equal to generate then execute it in the normal way
    x = x.split(",")
    list_of_num = [input_list for input_list in x]


else:
'''
If x is equal to "generate" then generate a list with random values and print the
maximum of them
'''
    list_of_num = random.sample(range(-1000000, 1000000), 4)

maximum_output_number = list_of_num[0]

for y in range(len(list_of_num)):

    # if the other element is max then last element.
    if list_of_num[y] > maximum_output_number:
        maximum_output_number = list_of_num[y]
print(maximum_output_number)
Sign up to request clarification or add additional context in comments.

Comments

1

Can be done in just a couple lines using the following:

import random
ip = input("Input 4 numbers separated by comma's to find the lowest and highest numbers:\n").split(",")
max_num = max([int(ip[i]) if ip[0] != 'generate' else random.randint(-1000000,1000000) for i in range(max(len(ip),4))])

Example:

>>> ip = input("Input 4 numbers separated by comma's to find the lowest and highest numbers:\n").split(",")
Input 4 numbers separated by comma's to find the lowest and highest numbers:
4,12341,-7029704,9143750
>>> max_num = max([int(ip[i]) if ip[0] != 'generate' else random.randint(-1000000,1000000) for i in range(max(len(ip),4))])
>>> max_num
9143750
>>> ip = input("Input 4 numbers separated by comma's to find the lowest and highest numbers:\n").split(",")
Input 4 numbers separated by comma's to find the lowest and highest numbers:
generate
>>> max_num = max([int(ip[i]) if ip[0] != 'generate' else random.randint(-1000000,1000000) for i in range(max(len(ip),4))])
>>> max_num
837861

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.