7.6 LAB: Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or any string with a non-integer character, the output is: no

Respuesta :

Limosa

Answer:

Following are the program in the Python Programming Language:

def check(num): #define function

 if(num.isdigit()): #set if statement

   return "yes"; #return yes if condition is true

 else:  

   return "no";# return no if condition is false

string=input("Enter the numbers 0-9: ") #get input from the user

print(check(string)) #print and call the function

Output:

Enter the numbers 0-9: 1995

yes

Enter the numbers 0-9: 42,000

no

Explanation:

Here, we define a function "check()" and pass an argument in its parameter "num" inside the function.

  • we set if-else conditional statement and check the value stored in the variable "num" is the digit by using built in function "isdigit()" then return "yes".
  • Then, otherwise it return "no".

Finally, we get the input from the user in the variable "string" then, we call the function through "print()" function.

The program illustrates the use of strings and string manipulations.

String manipulations involve carrying several operations and tests on a string.

The required program, where comments are used to explain each line is as follows:

#This gets input from the user

userInput=input("User input: ")

#This checks if the input contains only integer

if(userInput.isdigit()):

   #If yes, the program prints "yes"

   print("yes")

else:

   #If otherwise, the program prints "no"

   print("no")

The program tests the user input, and prints the appropriate output string.

See attachment for the complete program and the sample run

Read more about similar programs at:

https://brainly.com/question/24725082

Ver imagen MrRoyal
Q&A Education