Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

  1. 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" =>=>

  1. Read the input from the user(use input())
  2. Repeatedly read the input, until the user inputs a digit.
  3. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Core Ios Developer S Cookbook Core Recipes For Programmers

Authors: Erica Sadun ,Rich Wardwell

5th Edition

0321948106, 978-0321948106

More Books

Students also viewed these Programming questions

Question

=+ Identify the ethical dilemma in this scenario.

Answered: 1 week ago