Python Forum
Non Grade/School Help - PLEASE
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Non Grade/School Help - PLEASE
#1
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

Attached Files

.pdf   Problem 1-3 HELP.pdf (Size: 129.2 KB / Downloads: 258)
.docx   Problem 1-3 HELP.docx (Size: 15.83 KB / Downloads: 260)
Reply
#2
(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.
Reply
#3
Here is code I am stuck on.
area = height * width
# Display the output
print('The area of the rectangle = ' + str(area) + ' sq. units')
buran write Jun-18-2021, 04:48 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#4
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.
Reply
#5
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
buran write Jun-18-2021, 04:49 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#6
The error message is clear. Your retrurn statements is outside function. Fix that - probably you want print?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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~~~~~~~
Reply
#8
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.
buran write Jun-18-2021, 05:50 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#9
(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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Method to calculate average and grade ajitnayak1987 8 6,154 Apr-28-2022, 06:26 AM
Last Post: rayansaqer
  Calculating Grade Average IstvanCH 5 4,950 Jan-27-2019, 04:42 PM
Last Post: aakashjha001
  Student grade program help debug ccm1776 3 5,033 Nov-14-2018, 02:41 AM
Last Post: stullis
  Grade Loop dtweaponx 8 12,250 Oct-17-2017, 02:01 PM
Last Post: buran
  Help? Letter Grade assignment.. zepel 3 4,533 Apr-23-2017, 12:47 PM
Last Post: idontreallywolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020