Hi, please need help for python lab use in 3.42 shell Overview In this lab, you
ID: 639952 • Letter: H
Question
Hi, please need help for python lab use in 3.42 shell
Overview
In this lab, you will validate user input values to make sure that they make sense.
Create the starting point code for this lab:
def main():
name = 'Jack'
rating = 3
laugh = 'Bwa' + 'ha' * rating
print('My name is %s. %s.' % (name, laugh))
# call main
main()
What does this code do? See if you can figure it out before you type it in and run it.
Preliminary Work
Before we get started with the lab itself, let's update this so that the name and rating values are from the user.
Getting name from the user
The starting point code hard-codes the value 'Jack' for the variable name.
name = 'Jack'
Let's replace this with hard-coded value with a question for the user. We need a string here, so we'll use input.
name = input('Enter the name: ')
Getting rating from the user
The starting point code hard-codes the value 3 in for rating.
rating = 3
Let's replace this with a query to the user. The value needs to numeric, so, we'll use the input function with the int function.
rating = int(input('Enter the evilness rating: '))
This code works
Explanation / Answer
output of
def main():
name = 'Jack'
rating = 3
laugh = 'Bwa' + 'ha' * rating
print('My name is %s. %s.' % (name, laugh))
main()
is My name is Jack. Bwahahaha. as laugh is 'Bwa' + 'ha'*3 i.e 'Bwahahaha'
Code for above
def num(n):
for chars in n:
if (ord(chars) < 48 or ord(chars) > 57):
return False
return True
def main():
print ('Enter the name : ');
inp = input();
while (inp == ''):
print('Please Enter some name,it can not be empty')
inp = input()
print ("Enter the evilness rating: ")
rating = input()
while (num(rating) == False):
print("Error in input. Rating must be a non-negative integer.")
print ("Try again. Enter the evilness rating:")
rating = input()
rate = int(rating)
laugh = 'Bwa' + 'ha' * rate
print('My name is %s. %s.' % (inp, laugh))
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.