0% found this document useful (0 votes)
31 views6 pages

You Try!: "Enter The Second Number:" "Enter The Second Number:"

The document provides examples and explanations of various Python programming concepts. It includes questions about when the + operator concatenates strings versus adding numbers, why to use line continuation characters, how to write a basic program to calculate mathematical operations on user-input numbers, how to predict and check outputs of arithmetic expressions, and how to write programs to convert temperatures, calculate circle properties, convert binary to decimal, and generate a personalized text adventure story based on user inputs.

Uploaded by

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

You Try!: "Enter The Second Number:" "Enter The Second Number:"

The document provides examples and explanations of various Python programming concepts. It includes questions about when the + operator concatenates strings versus adding numbers, why to use line continuation characters, how to write a basic program to calculate mathematical operations on user-input numbers, how to predict and check outputs of arithmetic expressions, and how to write programs to convert temperatures, calculate circle properties, convert binary to decimal, and generate a personalized text adventure story based on user inputs.

Uploaded by

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

You Try!

1. When does the + sign concatenate rather than add?

It concatenates when are trying to join a string with another

2. Why use the line continuation character if the Python Interpreter does not care
how long a line is?

We do this for the code to be easily read and debugged,as it can appear all at once on the
editor without needing to move a lot to finish reading the line.

3. Write a program (using the Mu editor) that requests the user to enter two
numbers and prints the sum, product, difference and quotient of the two numbers.

num_1 = int(input("Enter the second number:"))#get number 1


from user
num_2 = int(input("Enter the second number:"))#get number 2
from user

print()
print("The sum of them is ",num_1+num_2)#add them and output
the result
print("The product of them is ",num_1*num_2)#multiply instead
this time
print("The difference between them is ",num_1-num_2)#subtract
to find difference
print("By dividing the first over the second,their quotient
is ",num_1/num_2)#divide for quotient

4. Predict the output of the following statements, then check using Python:

■ print( 6 + 3 * 5 – 1) 20
■ print( 2 * -2 ) -4
■ print( 9 / 2 ) 4.5
■ print( 9 // 2 ) 4
■ print( 9 // 2.0 ) 4.0
■ print( 9 % 2 ) 1
■ print( 7 + 3 * 6 / 2 – 1 ) 15.0
■ print( (6 + 2) * 3 ) 24
■ print( 14 // (7 – 1) ) 2
■ print( 2 % 2 + 2 * 2 – 2 / 2 ) 3.0
■ print( ( 2 ** 3 * ( 4 + ( 5 * 6 / ( 10 ) ) ) ) ) 56.0
■ print( float(2) + int(3.6) ) 5.0

After checking the results are the same

5. When does the + sign concatenate rather than add?

6. Write a program that converts Fahrenheit temperatures into Celsius. Your


program should prompt the user to enter the temperature in Fahrenheit, and then
display the temperature converted to Celsius, rounded to 1 decimal place. The
formula is: C = (F – 32) x 5/9Note: Be sure to use proper style: meaningful
variable names, comments etc.

fahrenheit = float(input('Enter the temperature in fahrenheit: '))


#take fahrenheit input from user
celsius = (fahrenheit-32)*5/9 #use the formula to convert to
celsius
print('It is ',round(celsius,1),'in celsius') #print rounded to one
decimal place

7. Write a program that reads in the radius of a circle and displays the circle's
diameter (2r), circumference (πd) and area (πr^2). Use the constant value
3.14159265 for π. Do these calculations in print statements, and round to 2
decimal places.

radius = float(input('Enter the radius of the circle: '))


#take a float variable of the radius
pi = 3.14159265 #constant value of pi

print("The diameter ",round(radius * 2,2)) #done calculation


in print func and rounded to 2 decimal places.
print("The circumference ",round(pi * radius * 2,2))#repeat
for circumference
print("The Area ",round(pi * radius * radius,2))#repeat for
Area

8. Write a program that accepts a binary number from the user (as a string) and
converts it to decimal (base-10). Hint: type help(int) in the Python shell to learn
other features of the int() function.

num = input("Enter a binary number: ")


#print(help(int)) # i found this from the help function

#class int(object)
# | int([x]) -> integer
# | int(x, base=10) -> integer
# |Base 0 means to interpret the base from the string as an
#integer literal.
# | >>> int('0b100', base=0)

decimal = int(num,base=0)
print(decimal)

9. Write a program that produces a personalized adventure about a band of explorers


who find 750 gold pieces.' The player should be asked for the number of explorers,
the number of explorers lost in battle, and the name of the quest leader.' The
program should then use the information to generate a basic adventure story.' Hint:
you'll need to use the remainder (%) operator.

Here's a sample run, assuming the user types 17, 13, and Ms. Tajalli the
Computer Teacher as input:

Welcome to Lost Fortune!

Please enter the following for your personalized adventure:

Enter the number of explorers: 17

Enter the number of explorers lost in battle: 13

Enter the name of the quest leader: Ms. Tajalli the Computer
Teacher

Here's your personalized story:

Ms. Tajalli the Computer Teacher led 17 adventurers on a quest for


gold.

The group fought a band of ogres and lost 13 members.

Only 4 survived.
The party was about to give up when they stumbled upon the buried

fortune of 750 gold pieces.

They split the loot and Ms. Tajalli the Computer Teacher kept the
extra 2 gold pieces.

print('Welcome to Lost Fortune!')


print('Please enter the following for your personalized
adventure:')
explorers = input('Enter the number of explorers: ')#keept as
string to be used in the story
lost = input('Enter the number of explorers lost in battle:
')
leader = input('Enter the name of the quest leader: ')

print('Here\'s your personalized story:')

story = []#this list will have all the lines of the story for
easier storing of each line on its own with proper spacing
story.append(leader + ' led ' + explorers + ' adventurers on
a quest for gold.')
story.append('The group fought a band of ogres and lost ' +
lost + ' members.Only ' + str(int(explorers) - int(lost)) + '
survived.')#changed explorers and lost to integers to be able
to calculate the difference and turned the difference to a
string after
story.append('The party was about to give up when they
stumbled upon the buried fortune of 750 gold pieces.' )
story.append('They split the loot and ' + leader + ' kept the
extra ' + str(750%(int(explorers) - int(lost))) + ' gold
pieces.')#same as line 10 # used % to know the reminder that
the leader will keep

for line in story:


print(line)#used a for loop to print each sentence on one
line for easier reading from the list

You might also like