Free form back to fixed form fortran [duplicate] - fortran

This question already has answers here:
Syntax error in call statement in Fortran
(1 answer)
invalid character name at (1)
(2 answers)
Line continuation of strings in Fortran
(4 answers)
Closed 1 year ago.
So I have to write this program in fortran77 wiith .f file extension and I dont want to compile it with an option. I want to fixed the error for but for some reason everythiing I've tried is still giving me this error. Ive included the code and the error terminal outputs
''' program p1
implicit none
integer :: choice
real :: inputValue
do while(choice /= 0)
print *, ' '
print *, 'Enter a conversion option (1-6 or 0 to exit):'
print *, '-------------'
print *, '(1) Pounds to Kilograms'
print *, '(2) Kilograms to Pounds'
print *, '(3) Feet to meters'
print *, '(4) Meters to feet'
print *, '(5) farenheit to Celsius'
print *, '(6) Celsius to Fahrenheit'
print *, '(0) Exit this progrm'
print *, '-------------'
READ(*,*) choice
IF (choice == 1) THEN
print *, 'Please enter the number (Integer) of Pounds to be converted into Kilograms'
READ(*,*) inputValue
inputValue = inputValue / 2.20
print *, 'Your value is:', inputValue
ELSE IF (choice == 2) THEN
print *, 'Please enter the number (Integer) of Kilograms to be converted into Pounds'
READ(*,*) inputValue
inputValue = inputValue * 2.20
print *, 'Your value is:', inputValue
ELSE IF (choice == 3) THEN
print *, 'Please enter the number (Integer) of Feet to be converted into Meters'
READ(*,*) inputValue
inputValue = inputValue / 3.28
print *, 'Your value is:', inputValue
ELSE IF (choice == 4) THEN
print *, 'Please enter the number (Integer) of Meters to be converted into Feet'
READ(*,*) inputValue
inputValue = inputValue * 3.28
print *, 'Your value is:', inputValue
ELSE IF (choice == 5) THEN
print *, 'Please enter the number (Integer) of Fahrenheit to be converted into Celsius'
READ(*,*) inputValue
inputValue = (5.0/9.0) * (inputValue - 32)
print *, 'Your value is:', inputValue
ELSE IF (choice == 6) THEN
print *, 'Please enter the number (Integer) of Celsius to be converted into Fahrenheit'
READ(*,*) inputValue
inputValue = (inputValue * 9.0/5.0) + 32
print *, 'Your value is:', inputValue
ELSE IF (choice == 0) THEN
print *, 'EXIT'
END IF
ENDDO
end program p1'''
Here is the error output image

Related

Why is the average not calculating correctly, and why does the average letter grade remain F no matter the number?

This is my code for the program that calculates 5 user inputs of test scores, displays the letter grade, and then calculates the average score and letter grade. However, the grade comes back as the wrong average, and the letter grade stays F no matter the number.
import os
#constants
PROG_ID = "\nFilename: Average_Grade.py\n"\
"Homework: 09\n"\
"Source Code Author: \n"\
"Reference: Chapter 5\n"\
"Date: 9 April 2022\n"
USER_PROMPT = "Please enter a test score. "
GRADE_TAG = "The letter grade is "
OUTPUT_TAG = "The average grade is "
#display program id
def progId ():
print(PROG_ID)
print()
#prompt user
def getInput(testScore, gradeList):
for item in range (5):
testScore = int(input(USER_PROMPT))
gradeList.append(testScore)
determineGrade(testScore)
print()
#letter grade
def determineGrade(testScore):
if testScore >= 90: print (GRADE_TAG, "A")
elif testScore >= 80: print (GRADE_TAG, "B")
elif testScore >= 70: print (GRADE_TAG, "C")
elif testScore >= 60: print (GRADE_TAG, "D")
else: print (GRADE_TAG, "F")
#show average
def calculateAverage(testScore, gradeList):
avg = sum(gradeList)/len(gradeList)
print(OUTPUT_TAG, format(avg, ".0f"))
avg = testScore
determineGrade(testScore)
def housekeeping():
input ("Press enter to exit.")
os._exit(1)
#control
def main():
#variables
testScore = avg = 0
gradeList = [testScore]
#calls
progId()
getInput(testScore, gradeList)
calculateAverage(testScore, gradeList)
main()
housekeeping()
If for some reason your main does not get called before the rest your gradeList is never initialised and calling methods on it would do nothing? I would initialise the array at the beginning of the file. You should test if at any point your array does not only contain 0, or nothing at all with a console.log().

How to find the min and max in a number list [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 6 years ago.
I'm having trouble with a project for class and I was looking for some help. I need to make a code where it will ask for numbers repeatedly and then print out the min and max in the list. My teacher said we can do this anyway we wish.
num_list = []
while True:
num = raw_input("Enter a number: ")
if num == "done" : break
if num >= 0:
num_list.append(num)
print min(num_list)
print max(num_list)
This is what I have so far, it asks for the number inputs and will break when I enter done, but it will only print out "Done" for the min and max in the list.
you need to convert the numbers to an integer or float type before calling min/max.
# under n >= 0:
num = int(num) # or num = float(num)
num_list.append(num)
so an example of working code would be:
num_list = []
while True:
num = raw_input("Enter a number: ")
if num == "done" :
break
try: # use a try except to verify user input is in fact a number
num = int(num) + 0 # alternatively use float(num) if non integer numerical inputs will be used
if num >= 0:
num_list.append(num)
print "min: ",min(num_list)
print "max: ",max(num_list)
except:
print "invalid input"
Not calling max/min every iteration:
num_list = []
_min, _max = None, None
while True:
num = raw_input("Enter a number: ")
if num == "done" :
break
try: # use a try except to verify user input is in fact a number
num = int(num) # alternatively use float(num) if non integer numerical inputs will be used
if num >= 0:
if not _min or not _max:
_min,_max = num, num
elif num < _min:
_min = num
elif num > _max:
_max = num
num_list.append(num)
except:
print "invalid input"
print "min:", _min
print "max:", _max

Why doesn't the menu in this Python program function?

I have looked over this code 100 times and I feel like it may be something small that I am missing. Program will allow you to login, and display the menu but after entering your selection it continuously displays the menu again.
import sys
def main():
login = 'yes'
choice = 0
hours = [0] * 7
wages = 0
totalHours = 0
totalWages = 0
print
login = raw_input('Do you want to login?')
while not (login == 'yes' or login == 'no'):
print
print 'Please enter a yes or no'
while login == 'yes':
print
userId = raw_input ('Enter user name:')
passWord = raw_input('Enter password:')
while passWord != userId:
print 'Incorrect Password, please try again.'
passWord = raw_input('Enter password:')
while passWord == userId:
print 'Login Success!'
print
print 'Enter 1 to view upcoming schedule'
print 'Enter 2 to view previous schedule'
print 'Enter 3 to calculate wages due'
print 'Enter 4 to verify clock in/out times'
print
choice = raw_input('Enter 1 to 4 from menu.')
def readFromNext(nextWeek):
inFile = open('nextweek.txt', 'r')
str1 = inFile.read()
print str1
str2 = inFile.read()
print str2
print
inFile.close()
def readFromLast(lastWeek):
inFile = open('lastweek.txt', 'r')
str1 = inFile.read()
print str1
str2 = inFile.read()
print str2
print
inFile.close()
def getHours(hours):
counter = 0
while counter < 7:
hours[counter] = input('Enter hours worked per day')
counter = countr + 1
return hours
def getTotalHours(hours, totalHours):
counter = 0
while counter < 7:
totalHours = totalHours + hours[counter]
counter = counter + 1
return totalHours
def getWages(wages):
wages = input('Enter your hourly wage.')
return wages
def getTotalWages(totalHours, wages):
totalWages = totalHours * wages
print 'Your total pay due is:' , totalWages
return totalWages
def readFromClock(clockHours):
inFile = open('clockhours.txt', 'r')
str1 = inFile.read()
print str1
str2 = inFile.read()
print str2
print
inFile.close()
while choice != '5':
if choice == '1':
readFromNext(nextWeek)
print 'Upcoming schedules'
if choice == '2':
readFromLast(lastWeek)
print 'Previous schedules'
if choice == '3':
hours = getHours(hours)
totalHours = getTotalHours(hours, totalHours)
wages = getWages(wages)
totalWages = gettotalWages(totalHours, wages)
print 'Paycheck calculator'
if choice == '4':
readFromClock(clockHours)
print 'Clock in/out times'
main()
What do you expect it to do? You have added many functions but they appear to be unused. You end up printing the menu over and over again because you are in the loop
while passWord == userId:
...
You have to do something with the choice variable and break out of that loop if you want anything else to happen.
For example:
choice = raw_input('Enter 1 to 4 from menu.')
if choice == 1:
readFromNext(nextWeek)
elif choice == 2:
readFromLast(lastWeek)
elif choice == 3:
getTotalWages(totalHours, wages)
elif choice == 4:
getHours(hours)
Obviously you will need to figure out which function maps to which user input, but currently you are not doing anything with the user's choice, merely asking them to choose over and over again with no control over the termination of the loop.
You are not changing the value of login in your while not loop.
login = 'default'
while not (login == 'yes' or login == 'no'):
print 'Please enter a yes or no'
login = raw_input('Do you want to login?')
... for example. The way you currently have it is never going to end:
while not (login == 'yes' or login == 'no'):
print
print 'Please enter a yes or no'
This is conceptually identical as:
while True:
if login == 'yes' or login == 'no':
break
print
print 'Please enter a yes or no'
So if the login is never changed, it will never end.

Python: Program for getting maximum and minimum value without using max and min function

I wrote the code below for getting max and min values as part of my MOOC assignment. This program continuously takes input from the user until the user types 'done'.
Once 'done' is typed, the program gives the results for max and min values. The problem is that the result for max value is always correct, but the result for min value is always "None".
largest = None
smallest = None
while ( True ) :
inp = raw_input('Enter a number: ')
if inp == 'done' :
break
try:
inp = float(inp)
except:
print 'Invalid input'
continue
if inp is None or inp > largest:
largest = inp
if inp is None or inp < smallest:
smallest = inp
print largest, smallest
The code you posted gives None for both largest and smallest. There is a continue statement after you try catch, so obviously it just keeps taking input and never terminates. continue will tell the loop to skip to the next iteration. So the continue has to come in the except block (this is probably an indentation mistake). Secondly, you are comparing input with None. I guess that was a typo in you if condition (it should be 'if largest is None' not 'if inp is None')
Modified code: (check last 2 if conditions):
largest = None
smallest = None
while ( True ) :
inp = raw_input('Enter a number: ')
if inp == 'done' :
break
try:
inp = float(inp)
except:
print 'Invalid input'
continue
if largest is None or inp > largest:
largest = inp
if smallest is None or inp < smallest:
smallest = inp
print largest, smallest

How to calculate average / mean on python 2.7

Im trying to calculate the average when an user will enter the numbers and will stop when he decide. I manage to get the sum however I cant divided it because I need somehow get the quantity of the inputs before he stops entering. Here is my code so far:
print (" please enter all the numbers you want to calculate the avarage. After you enter all of them press 'f' to finish.")
s = 0
x = raw_input ('please enter the number')
while (num != 'f'):
x = eval (x)
s = s + x
x = raw_input ('please enter the number')
print (' the average is'), s
Like you see I manage to get the sum however I dont know how I can get the quantity of the input to then divide the sum with this to get the average. Any help will be appreciated
You just needed to add a counter inside your while loop:
print (" please enter all the numbers you want to calculate the avarage. After you enter all of them press 'f' to finish.")
s = i = 0 #declare counter: i = 0
x = raw_input ('please enter the number')
while (x != 'f'):
x = eval (x)
s = s + x
i+=1 #increment counter: i=i+1 or i+=1 (these 2 options are equivalent)
x = raw_input ('please enter the number')
print (' the average is'), s/i #divide the sum by counter