Python Forum
Non Grade/School Help - PLEASE - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Non Grade/School Help - PLEASE (/thread-34010.html)



Non Grade/School Help - PLEASE - gbyrne12 - Jun-17-2021

Hello, stuck on a few issues for 3 problems for a online do it yourself course. Code and Comments from instructor attached. Any help greatly appreciated. Thank You in advance

Posted both word and PDF. Thanks


RE: Non Grade/School Help - PLEASE - snippsat - Jun-17-2021

(Jun-17-2021, 09:29 PM)gbyrne12 Wrote: Hello, stuck on a few issues for 3 problems for a online do it yourself course. Code and Comments from instructor attached. Any help greatly appreciated. Thank You in advance

Posted both word and PDF. Thanks
This is not all how it work here,no one will just to these task for you without any effort on your side.
You have to give it try and then post code and explain where you are stuck.


RE: Non Grade/School Help - PLEASE - gbyrne12 - Jun-17-2021

Here is code I am stuck on.
area = height * width
# Display the output
print('The area of the rectangle = ' + str(area) + ' sq. units')



RE: Non Grade/School Help - PLEASE - snippsat - Jun-17-2021

Look at this for using code tags.
To make that code work need to set some variables.
height = 10
width = 15
area = height * width
# Display the output
print('The area of the rectangle = ' + str(area) + ' sq. units')
# Or the better way use f-string
print(f'The area of the rectangle = {area} sq. units')
Output:
The area of the rectangle = 150 sq. units The area of the rectangle = 150 sq. units
A quick look at the task so should you make function of this,look at some tutorials about making functions in Python.


RE: Non Grade/School Help - PLEASE - gbyrne12 - Jun-18-2021

Thanks, also stuck on this one:

In the code cell below, create a method that convert temperature from fahrenheit to celcius and another method that does the reverse.
The beginning of the methods is given below:

def celToFah(celcius):
  fahrenheit = (celcius * 9/5) + 32
  print('%.2f celcius is: %0.2f fahrenheit' %(celcius, fahrenheit))

def fahToCel(fahrenheit):
  celcius = (fahrenheit - 32) * 5/9
  print('%.2f fahrenheit is: %0.2f celcius' %(fahrenheit, celcius))

# Get the temperature in celcius from user and display equivalent temperature in fahrenheit
cel = float(input('Enter the temperature in celcius:'))
return 'The temperature in Fahrenheit = ' + str(celToFah(cel))
 
# Get the temperature in fahrenheit from user and display equivalent temperature in celcius
fah = float(input('Enter the temperature in fahrenheit: '))
print('The temperature in celcius = ' + str(fahToCel(fah)))
Error:
Enter the temperature in celcius:10 File "<ipython-input-5-89452f5b965c>", line 12 return 'The temperature in Fahrenheit = ' + str(celToFah(cel)) ^ SyntaxError: 'return' outside function



RE: Non Grade/School Help - PLEASE - buran - Jun-18-2021

The error message is clear. Your retrurn statements is outside function. Fix that - probably you want print?


RE: Non Grade/School Help - PLEASE - snippsat - Jun-18-2021

Can clean it up and give some tips.
def cel_to_fah(celcius):
    fahrenheit = (celcius * 9/5) + 32
    return fahrenheit

def fah_to_cel(fahrenheit):
    celcius = (fahrenheit - 32) * 5/9
    return celcius

if __name__ == '__main__':
    cel = float(input('Enter the temperature in celcius: '))
    print(f'The temperature in Fahrenheit: {cel_to_fah(cel:.2f)}')

    fah = float(input('Enter the temperature in fahrenheit: '))
    print(f'The temperature in celcius: {fah_to_cel(fah):.2f}')
Output:
Enter the temperature in celcius: 0 The temperature in Fahrenheit: 32.00 Enter the temperature in fahrenheit: 32 The temperature in celcius: 0.00

Just return from functions as you us print() with explanation later.
See that indentation is 4-space and no CamelCase in Python,look at PEP-8

Look at this link Python 3's f-strings,it give code more cleaner look,
and as new(or old) Python user f-string is what should be used.
>>> for word in 'f-strings are awesome'.split():
...     print(f'{word.upper():~^20}')
~~~~~F-STRINGS~~~~~~
~~~~~~~~ARE~~~~~~~~~
~~~~~~AWESOME~~~~~~~



RE: Non Grade/School Help - PLEASE - gbyrne12 - Jun-18-2021

Thanks so much. I got thru the rest successfully. This was my last issue out of the 20 + problems.

Make sure you have read Chapter 6 from the Think Python book.
Write a boolean function that receives a number as a parameter and checks if it's even or odd, and display an appropropriate method.

Here is the expected output:
Sample run 1:
Enter a number: 23 Your number is odd!
Sample run 2:
Enter a number: 44 Your number is even!

# check if the input number is odd or even
# a number is even if division by 2 gives a reminder of 0
# if the remainder is 1, it is an odd number

number = int(input("Enter a number: "))
if (number % 2) == 0:
return Your number is even!
else:
return Your number is odd!

File "<ipython-input-4-3a7b155c37c3>", line 7 return Your number is even! ^ SyntaxError: invalid syntax

Comments: Again, this code needs to be within a function.


RE: Non Grade/School Help - PLEASE - snippsat - Jun-19-2021

(Jun-18-2021, 11:59 AM)gbyrne12 Wrote: Comments: Again, this code needs to be within a function.
Yes it's the same as task over return most be inside a function,fill in the rest.
def odd_even(number):
    pass

if __name__ == '__main__':
    number = int(input("Enter a number: "))
    print(odd_even(number))