-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_while.py
More file actions
54 lines (46 loc) · 1.95 KB
/
input_while.py
File metadata and controls
54 lines (46 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ***************************************************************
# Name : input_while
# Author: Than Tong
# Created : * Course: CIS189
# Version: 1.0
# OS: Windows 11
# IDE: Python
# Copyright : This is my own original work
# based onspecifications issued by our instructor
# Description :
# Input: ADD HERE XXX
# Ouput: ADD HERE XXX
# Academic Honesty: I attest that this is my original work.
# I have not used unauthorized source code, either modified or
# unmodified. I have not given other fellow student(s) access
# to my program.
# This script collects numbers from the user within the range of 1 to 100.
# It appends valid numbers to a list until the user inputs -1, then it prints the list.
# Initialize an empty list to store the numbers
numList = []
# Define the sentinel value for user input
sentinelValue = -1
# Prompt the user to enter a number and store it in the variable 'number'
number = int(input("Enter a number (-1 to stop): "))
# Continue the loop as long as 'number' is not equal to the sentinel value
while number != sentinelValue:
# Continue the loop as long as 'number' is not within the range of 1 to 100
while not (1 <= number <= 100):
# Prompt the user to enter a number between 1 and 100 and store it in 'number'
number = int(input("Enter a number between 1 and 100: "))
# Add the valid number to the list
numList.append(number)
# Prompt the user to enter another number and update 'number'
number = int(input("Enter a number (-1 to stop): "))
# Print the list using a for loop
for i in numList:
print(i, end=" ")
# Test cases
# Test Case 1: Input 55, 12, 101, 3, -1
# Observed Output: 55 12 3
# Test Case 2: Input 200, 50, 75, 0, 25, -1
# Observed Output: 50 75 25
# Test Case 3: Input -1
# Observed Output: (No numbers entered)
# Test Case 4: Input 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, -1
# Observed Output: 5 10 15 20 25 30 35 40 45 50