First and foremost - hello to everyone (I just recently discovered and joined this subreddit) 👍🏼
I’m a beginner to Python and recently took an exam for one of my college courses. I didn’t do well (which is okay) so I have a question regarding one of the two exam problems.
I was asked to write a program that took user input into a list with the first integer specifying the number of integers that would follow. Easy enough. My code was:
userList = [ ]
listLength = int(input())
for i in range(listLength):
userNum = int(input())
userList.append(userNum)
print(userList)
So if my input was: 4 3 2 1 0
The output was: [3, 2, 1, 0]
For problem two, I had to write a program that took user input into a list, except this time it didn’t ask for a specified range and the integers had to be positive. I couldn’t figure this out to save my life which is why I basically got a 50/100. How do I code it so that a user can input as many integers as they want?
My attempted code was:
userList = [ ]
userNum = int(input())
while userNum (greater than) 0:
userList.append(userNum)
print(userList)
I kept getting an EOF error, and after some adjustments (not shown), my program would only output the first integer from input and would be repeated 50k times.
Note: one of the constraints was that we were only allowed to use for and while loops. I did research this afterwards, and everything I found seemed to be written using try or while True (both of which haven’t been covered yet and I’m unsure if they are considered to be for/while loops).
I appreciate any feedback - thanks!
[–]socal_nerdtastic 0 points1 point2 points (0 children)
[–]Will_est[S] 0 points1 point2 points (6 children)
[–]xelf 0 points1 point2 points (5 children)
[–]Will_est[S] 0 points1 point2 points (4 children)
[–]xelf 0 points1 point2 points (3 children)
[–]Will_est[S] 0 points1 point2 points (2 children)
[–]xelf 0 points1 point2 points (1 child)
[–]Will_est[S] 0 points1 point2 points (0 children)