-3

I'm currently trying to store user input as integers without appending them to a list or creating a list at all.

First off, I tried using 5 independent variables for each input (code below), and when this code is ran it gives something to the tune of:

The fahrenheits you entered are (1, 2, 3, 4, 5)

How would I go about removing those parentheses?

firstFahr = int(input("Please enter a Fahrenheit temperature: "))
secondFahr = int(input("Please enter a Fahrenheit temperature: "))
thirdFahr = int(input("Please enter a third Fahrenheit temperature: "))
fourthFahr = int(input("PLease enter a fourth Fahrenheit temperature: "))
fifthFahr = int(input("Please enter a fifth Fahrenheit temperature: "))

enteredFahrs = firstFahr, secondFahr, thirdFahr, fourthFahr, fifthFahr


print("The fahrenheits you entered are", enteredFahrs)

Thanks for any help in advance and apologies if this seems like a noob question, as I'm quite new to Python.

  • 4
    Why do you want to avoid a list? A list is actually the sanest thing to use, here. (Note that in the code you pasted, there's no list - just a tuple.) – Reinderien Sep 21 '19 at 02:04
  • Typically to store user input, common data structures such as a list, tuple, set, or dictionary are used. Currently you're storing the input as a tuple. I see no downside with using a list to store the variables . You can create a list like this `enteredFahrs = [firstFahr, secondFahr, thirdFahr, fourthFahr, fifthFahr]` – nathancy Sep 21 '19 at 02:21

3 Answers3

0

How about this:

prompts = ('first', 'second', 'third', 'fourth', 'fifth')
entered_fahrs = tuple(
   int(input(f'Please enter a {p} Fahrenheit temperature: '))
   for p in prompts
)
print(f'The Fahrenheits you entered are: {", ".join(str(f) for f in entered_fahrs)}')

If you really, truly want to avoid sequences, you can then do a simple unpack:

first_fahr, second_fahr, third_fahr, fourth_fahr, fifth_fahr = entered_fahrs
Reinderien
  • 11,755
  • 5
  • 49
  • 77
0

This should solve your problem:

firstFahr = int(input("Please enter a Fahrenheit temperature: "))
secondFahr = int(input("Please enter a Fahrenheit temperature: "))
thirdFahr = int(input("Please enter a third Fahrenheit temperature: "))
fourthFahr = int(input("PLease enter a fourth Fahrenheit temperature: "))
fifthFahr = int(input("Please enter a fifth Fahrenheit temperature: "))

print("The fahrenheits you entered are", firstFahr, secondFahr, thirdFahr, fourthFahr, fifthFahr)

There are no lists whatsoever (and no parentheses as well).

lenik
  • 23,228
  • 4
  • 34
  • 43
0

I doubt this is what you are really asked to do, but an alternative approach is to use a generator expression to avoid storing the variables entirely.

user_inputs = (
   int(input(f'Please enter a {p} Fahrenheit temperature: '))
   for p in ('first', 'second', 'third', 'fourth', 'fifth')
)

print("The fahrenheits you entered are", *user_inputs)
GZ0
  • 4,055
  • 1
  • 10
  • 21