0% found this document useful (0 votes)
55 views7 pages

Python Temperature and Conditionals Program

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)
55 views7 pages

Python Temperature and Conditionals Program

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

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= [Link](",")
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=[Link](",")
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).

Common questions

Powered by AI

Boolean expressions are fundamental to decision-making in Python, allowing for conditional execution based on multiple criteria. For example, decisions about survival in a game might depend on combined conditions such as the number of 'hits' being greater than a threshold and a 'shield' value being zero. Boolean operations can also be used to determine the possibility of 'escape' based on directional variables. These logical evaluations make program flows dependent on dynamic criteria, enhancing flexibility and functionality.

Python can be used to filter data items by iterating through a list of user-provided strings and checking each one’s length using a for loop. If a string is found to be four letters long, it is printed. This showcases Python’s ability to handle string operations and conditional logic to process and display only specific data entries from broader inputs.

User authentication is crucial for ensuring security by verifying user identity against a set of valid credentials. In Python, a simple method involves checking if an entered 'login id' exists within a list of approved users. Although rudimentary, it demonstrates basic authentication principles used in more complex systems. This approach highlights the importance of managing access to systems and sensitive information.

In Python, lists serve as versatile data structures that can be used to store and compare sequential data. By comparing a 'ticket' list to a 'lottery' list, a program can determine if a user has won. This involves splitting a user input string into list elements and checking equality with the winning list. Such comparisons enable the design of systems where outcomes depend on matching sequences, playing a critical role in gaming and probability-based applications.

Handling user inputs with Python's conditional structures enhances interface responsiveness by allowing real-time, dynamic interactions. Conditional logic checks inputs against set conditions to deliver customized responses, such as verifying login credentials or determining list membership. This results in applications that adapt to user behavior and provide instantaneous feedback, critical for seamless user experiences.

The conversion from Fahrenheit to Celsius can be implemented in Python using the formula \( C = \frac{5}{9}(F - 32) \). This involves taking a temperature value in Fahrenheit provided by the user, performing arithmetic operations to convert it, and printing the result. The significance of this lies in understanding how to manipulate and convert data types, handle user input, and perform arithmetic operations, which are fundamental programming skills.

Conditional statements determine leap years by checking divisibility by 4, a fundamental criterion of the Gregorian calendar. If a year is divisible by 4 and fails other specific century rules, it may be a leap year. Python uses an 'if' statement to express this condition and an 'else' statement for years that do not qualify. This logical method reflects the historic computation rules that maintain our calendar system by aligning it with the Earth's orbit.

Loop constructs in Python, such as the 'for' loop, allow the generation of numeric sequences by iterating over ranges. For example, executing a loop from 0 to 9 or 0 to 1 prints each integer in the sequence on a new line. These constructs are essential for tasks requiring repetitive actions or processing of elements in a sequence, such as simulations, data processing, or iterative calculations.

Python uses 'if' statements to evaluate conditions and execute commands based on their truth value. For instance, to check if a user is eligible for pension benefits, Python evaluates whether the 'age' is greater than 62. Similarly, using a list, it checks if an entered name exists within a predefined list of top baseball players to provide relevant feedback. This approach allows programs to dynamically respond to user input using logical structures.

Logical operators are pivotal in evaluating scenarios requiring multiple condition checks, such as escape possibilities where directional attributes determine outcomes. By using 'or' operators, Python evaluates whether at least one of the variables (north, south, east, west) is true, indicating a viable escape path. This technique exemplifies the complex decision-making processes in programming where multiple factors must be considered simultaneously.

You might also like