Working with users in python

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter6: User-defined Functions
Section: Chapter Questions
Problem 22PE
icon
Related questions
Question
Working with users in python
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.
1. Create a function is_single_digit (text) that checks if the string parameter text is a digit. The function should return the
Boolean True or False
2. In your main program, get the input from the user (use input())
3. Repeatedly read the input, until the user inputs a digit. (use while)
4. 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
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 <target_var>.isdigit() function (e.g., "2". isdigit() will return True);
alternatively you can use string comparison such as "0" <= target <= "9".
Example Output
Your digit is 1
Example input
6
Example Output
Your digit is 6
Transcribed Image Text: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. 1. Create a function is_single_digit (text) that checks if the string parameter text is a digit. The function should return the Boolean True or False 2. In your main program, get the input from the user (use input()) 3. Repeatedly read the input, until the user inputs a digit. (use while) 4. 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 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 <target_var>.isdigit() function (e.g., "2". isdigit() will return True); alternatively you can use string comparison such as "0" <= target <= "9". Example Output Your digit is 1 Example input 6 Example Output Your digit is 6
CS 3000
!
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 function inside the 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
●
422020.2684366.qx3zqy7
LAB
ACTIVITY
4.20.1: LAB: Working with users
1 def is_single_digit(text):
2
3
4
5
6
###
The function expects
param: var_name (var_type)
The result is ...
||||||
7
8
9 if __name_
pass
_main__':
main.py
0/12
Load default template...
Transcribed Image Text:CS 3000 ! 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 function inside the 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 ● 422020.2684366.qx3zqy7 LAB ACTIVITY 4.20.1: LAB: Working with users 1 def is_single_digit(text): 2 3 4 5 6 ### The function expects param: var_name (var_type) The result is ... |||||| 7 8 9 if __name_ pass _main__': main.py 0/12 Load default template...
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning