PYTHON 3! SIMPLE QUESTION REVIEW. Would like to see if my answers to the questio
ID: 3579500 • Letter: P
Question
PYTHON 3! SIMPLE QUESTION REVIEW. Would like to see if my answers to the questions are correct or are missing anything. Thank you
Q1:Write a function that accepts a string as an argument and displays the string backwards.
My code:
def print_reversed(s):
for letter in reversed(s):
print(letter)
print_reversed(input('Please enter a word: ')) #WOULD LIKE TO PRINT IN A LINE (HORIZONTALLY) INSTEAD OF VERTICALLY
__________________________________________________________________________________________________
Q2. Explain the difference between "is" and "==". Give an example (different than that used in the chapter 2) that illustrates items that return False for "is" and True for "==".
My answer/code:
"is" will return "TRUE" if two var point to the same object whereas "==" if the objects pointed to by the vars are equal
>>>1000 is 10**3
FALSE
>>>1000 == 10**3
TRUE
__________________________________________________________________________________________________________________
Q3. Write a loop that counts the number of space characters that appear in a the string referenced by mystring.
My code:
mystring = 'Mon Tue Wed Thur'
space_count = 0
for space in mystring:
if space == " ":
space_count +=1
print(space_count, "spaces")
___________________________________________________________________________________________________________________
Q4. Write an if statement that assigns 20 to the variable y and assigns 40 to the variable z if variable x is greater than 100
My code:
if (x > 100):
y = 20
z = 40
_________________________________________________________________________________________________________________
Q5. Write nested decision statements(structures) that perform the following: If amount1 is greater than 10 and amount2 is less than 100, display the greater of amount1 and amount2.
My code:
amount1 = input('Enter amount for amount1: ')
amount2 = input('Enter amount for amount2: ')
if (amount1 > 10) and (amount2 < 100):
if amount1 == amount2:
print(amount1, 'equals', amount2)
elif amount1 > amount2:
print(amount1, 'is greater than amount2')
else:
print(amount2, 'is greater than amount1')
else:
print('Amounts not within valid range')
Explanation / Answer
Q1 : changed to print horizontally
CODE:
def print_reversed(s):
for letter in reversed(s):
print(letter, end='') #end is used to change the default setting of printing from vertically to horizontally
print_reversed(input('Please enter a word: '))
OUTPUT:
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
Q2: This is working good.
CODE:
print(1000 is 10**3)
print(1000 == 10**3)
OUTPUT:
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
Q3: Working good.
CODE:
mystring = 'Mon Tue Wed Thur'
space_count = 0
for space in mystring:
if space == " ":
space_count +=1
print(space_count, "spaces")
OUTPUT:
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
Q4: Working good. Just added print statements to check the output
CODE:
x = 101
if (x > 100):
y = 20
z = 40
print("y = " , y)
print("z = ", z)
OUTPUT:
----------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
Q5: Just parsed the input to integer data type, otherwise the code was throwing runtime exception
CODE:
amount1 = int(input("Enter amount for amount1: "))
amount2 = int(input(" Enter amount for amount2: "))
if(amount1 > 10 and amount2 < 100):
if(amount1 == amount2):
print(" ",amount1, "equals", amount2)
elif(amount1 > amount2):
print(" ",amount1, 'is greater than amount2')
else:
print(" ",amount2, 'is greater than amount1')
else:
print("Amounts not within valid range")
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.