Question
6.22 LAB: Working with users Learning Objectives In this lab, you will : Use the while loop to repeat action an unknown number of times
6.22 LAB: Working with users
Learning Objectives
In this lab, you will :
- Use the while loop to repeat action an unknown number of times
- Practice input() statement inside the loop
- Learn complex string comparison conditions inside the while loop
- Create a function with a given name and parameter and given return values
- Use if/else statements inside the function to achieve different return statements by condition
- Use int() integer conversion function to have the number from string
Instructions
A keyboard can be really tricky, if it's broken! You try to enter a number but it prints a letter again and again! Let's try to fix that.
- Create a function is_this_digit(input_line) that checks if the string parameter is a digit. The function should return True or False
1.1. The string contains only one digit if the string length is 1 and the character inside is a digit character.
1.2. To check if the character is a digit character use .isdigit()function (e.g., "2".isdigit() will return True); alternatively you can use string comparison such as "0" =>=>
- Read the input from the user(use input())
- Repeatedly read the input, until the user inputs a digit.
- When they finally provide a digit as an input, then output it as follows: Your digit is DDD (where DDD is replaced by the provided digit).
Note that the program execution stops as soon as the user provides a single digit.
Example input
s
a
w
1
Example Output
Your digit is 1
Example input
6
Example Output
Your digit is 6
Example input
d
g
!
w
g
d
e
4
Example Output
Your digit is 4
Hints!
- When we cannot predict how many times we will need to repeat an action, that is when we use a while statement.
- Call your functioninsidethe while statement or save your function result each time and use that result in a while statement.
- Only when the execution gets out of the while loop (i.e., the loop-continuation-condition is False), you know that the program can print the Your digit is ....
This is what was given, please finish the rest (note that answer should be in python):
def is_this_digit(input_line): # Define your function here pass
if __name__ == '__main__': # Type your code here.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started