0% found this document useful (0 votes)
128 views

Python Tutorial 6

The document provides examples of code that takes user input and performs conditional checks and printing of output messages. It includes code samples that: 1) Request a login ID from the user and check if it is in a list of valid users, printing a success or failure message. 2) Ask the user to enter a list of words and print only the 4-letter words. 3) Demonstrate for loops that print sequences of numbers for different ranges.

Uploaded by

queen setilo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Python Tutorial 6

The document provides examples of code that takes user input and performs conditional checks and printing of output messages. It includes code samples that: 1) Request a login ID from the user and check if it is in a list of valid users, printing a success or failure message. 2) Ask the user to enter a list of words and print only the 4-letter words. 3) Demonstrate for loops that print sequences of numbers for different ranges.

Uploaded by

queen setilo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Implement a program that requests the current temperature in degrees Fahreneit

from the user and prints the temperature in degrees Celsius using the formula
5
Celsius = 9 (Fahrenheit – 32)

Your program should execute as follows:

Expected Output:

Enter the temperature in degrees Fahrenheit:50

The temperature in degrees Celsius is 10.0

Syntax:
F= input("Enter the temperature in degrees
Fahrenheit:")

C= (5/9) * (float(F)-32)

print("The temperature in degrees Celsius is " +


str(C))

OUTPUT:
Translate these conditional statements into Python if statements:

Expected Output:

(a) If age is greater 62, print ‘You can get your pension benefits’.

(b) If name is in list [‘Musial’, ‘Aaron’, ‘Williams’, ‘Gehrig’, ‘Ruth’], print ‘ One
of the top 5 baseball players, ever!’.

Syntax:

(a).
age=eval(input("Enter age:"))
if age > 62:
print("You can get your pension benefits.")

(b).
players=input("Enter name:")
list=["Musial","Aaron","Williams","Gehrig","Ruth"]
if players in list:
print("One of the top 5 baseball players, ever!")

OUTPUT:

(a).

(b).
Expected Output:

(c) If hits is greater than 10 and shield is 0, print ‘You are dead…’.

(d) If at least one of the Boolean variables north, south, east, and west is True, print
‘ I can escape.’.

Syntax:

(c).
hits=eval(input("Enter hits value:"))
shield=eval(input("Enter shield value:"))
if (hits > 10) and (shield == 0):
print("You are dead...")
(d).
north = True
south = True
east = False
west = False

if north | south | west | east:


print('I can escape.')
w/ user input:
direction=input("Enter direction:")
north = True
south = True
east = False
west = False

if bool(direction) == north | south | west | east:


print('I can escape.')

OUTPUT:

(c). (d).
Translate these into Python if/else statements:

Expected Output:

(a) If year is divisible by 4, print ‘Could be a leap year.’, otherwise print


‘Definitely not a leap year.’

(b) If list ticket is equal to list lottery, print ‘You won!’; else print ‘Better luck next
time…’

Syntax:

(a).
year=eval(input("enter year:"))
if year % 4 == 0:
print("could be a leap year")
else:
print("definitely not a leap year.")

(b).
ticket=(input("Enter ticket:"))
splittik= ticket.split(",")
lottery=["0","9","2","5"]
if splittik==lottery:
print("You won!")
else:
print("Better luck next time...")

OUTPUT:

(a). (b).
Implement a program that starts by asking the user to enter a login id (i.e., a
string).

The program then checks whether the id entered by the user is in the list [‘joe’,
’sue’, ‘hani’, ‘sophie’] of valid users.

Depending on the outcome, an appropriate message should be printed. Regardless


of the outcome, your function should print ‘Done.’ Before terminating.

Here is an example of a successful login:

Expected Output:

Login: joe

You are in!

Done.

And here is one that is not:

Login: john

User unknown.

Done.

Syntax:
loginid= input("Login:")
list=["joe","sue","hani","sophie"]
if loginid in list:
print("You are in!")
print("Done.")
else:
print("User unknown")
print("Done.")

OUTPUT:
Implement a program that requests from the user a list of words (i.e., strings) and
then prints on the screen, one per line, all four-letter strings in the list.

Expected Output:

Enter word list:[‘stop’,’desktop’,’top’, ‘post’]

stop

post

Syntax:
word=input("Enter word list:")
list=word.split(",")
for x in list:
if (len(x)==4):
print(x)

OUTPUT:
Write the for loop that will print these sequences of numbers, one per line, in the
interactive shell.

Expected output:

(a) Integer from 0 to 9 (i.e., 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

(b) Integer from 0 to 1 (i.e., 0, 1)

(a).
print("Integer from 0 to 9")
for x in range(10):
print (x)

(b).
print("Integer from 0 to 1")
for x in range(2):
print (x)

OUTPUT:

(a). (b).

You might also like