Python Forum
Can't figure out how to write the correct functions for my pseudocode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't figure out how to write the correct functions for my pseudocode
#1
I'm having some issues figuring out how to write the correct functions to turn my pseudocode in a Python program. These are the full instructions:
Write a Python program to make and cancel reservations on a single route on a single day. Here are core specifications for the software.
1.Input. The input will be read from a text file named transactions.csv. Each line will havea single integer. The first number is the beginning number of seats available for theroute and must be non-negative.
2.Starting Availability. The availableStart is the first integer from the input.
3.Reservations. The number of reservations is computed as the sum of all positiveintegers from the input, except the first number of the input is not included.
4.Cancellations. The number of cancellations is computed as the sum of the negativeintegers from the input, except the first number of the input is not included.
5.Ending Availability. The availableEnd is computed as availableStart minus reservationsminus cancellations. Note that subtracting the positive sum of reservations subtractsthem from availability and subtracting the negative sum of cancellations adds them backinto availability.
6.Output. The output will be one printed line in CSV format:availableStart,reservations,cancellations,availableEndNote that cancellations should be a negative number.

Below is the code I have so far and I know that it's not correct because I'm getting syntax errors when I begin the code after importing the csv. I'm sure this is probably a simple task, but I have no background in coding and this is just for a computer science 101 class. Any help or tips would be greatly appreciated!

import csv
with open("transactions.csv", 'r') as csvfile:
 reader = csv.reader(csvfile)
 for row in reader:
     print(row)

 [error]for ('availableStart') input[0]
 #first integer from csv[/error]
 for ('reservations') input[0]:
     #initialize reservations variable
 for ('cancellations') input[0]:
     #initialize cancellations variable
 for each input starting from the second:
     if input is positive:
         #if its positive then its new reservations
         reservations = "reservations" + input
     else:
         #if its not positive then its new cancellations
         cancellations = "cancellations" + input

 for ('availableEnd') = reservations + cancellations:
     write output as availableStart,reservations,cancellations,availableEnd
     output as csvfile:
         reader = mrodick3_Output
         
exit
#End Program
 
Reply
#2
please provide enough sample data (truncated 'transactions.csv') to recreate syntax error.
Reply
#3
You're getting syntax errors because you are running pseudocode, not Python code. You have several for loops like this:

for ('reservations') input[0]:
That's not how for loops are written in python. And looking at the pseudocode, that is probably meant to be a conditional (if statement) rather than a loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting pseudocode to Python qwertyK 3 23,561 Oct-24-2017, 01:41 PM
Last Post: qwertyK
  Problem writing code with my pseudocode MattWilk97 1 2,845 Aug-29-2017, 01:54 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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