Python Forum
Looping unknowns with user input hw question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping unknowns with user input hw question
#5
(Sep-30-2018, 09:06 AM)j.crater Wrote: Hello,
why are you using % (modulus) operator? I don't think you need it. It will print only floors satisfying this mathematical condition.
Hi, I used it because I was modified an example code from the chapter on the Grok learning site. It came close to the answer I needed in contrast to other variations of the code I made. I realize now that it was probably a bad idea and kept me from progression on the assignment.

(Sep-30-2018, 01:13 PM)gruntfutuk Wrote: As you know exactly the range required, it is easier to use a for loop than a while loop as it saves you having to write the code for counting.

How about:

floor = int(input("Current floor: "))
dfloor = int(input("Destination floor: "))
print('\n' + '\n'.join([f'Level {floor_}' for floor_ in range(floor, dfloor + 1)]))
This is using list comprehension inside of the print function call to generate a list of strings, each one being "Level " and a floor number. The floor numbers are generated by the for loop using a range starting from one and stopping before the destination floor + 1.

If you don't want to use list comprehension, and probably more clear anyway:

floor = int(input("Current floor: "))
dfloor = int(input("Destination floor: "))
print()
for floor_ in range(floor, dfloor + 1):
    print(f'Level {floor_}')

Thank you so much for this. I ended up changing your suggestion a little but it helped.
Thank you to everyone else as well for your help too~
Reply


Messages In This Thread
RE: Looping unknowns with user input hw question - by Turkejive - Sep-30-2018, 04:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,256 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,342 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 7,882 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 4,229 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,758 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,048 Nov-04-2019, 02:02 PM
Last Post: perfringo
  Question about running comparisons through loop from input value Sunioj 2 2,362 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  how to add user input to a dictionary to a graph KINGLEBRON 3 2,976 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,464 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,533 Mar-11-2019, 07:37 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020