finding the max value in a list and adding 1 - list

I have created some code to read data from a text file that has a list of sequential number. The user is prompted to see if they are a teacher or student(I have only written for a student) When they enter student the numbers in the text file are read to the list "log" and the maximum value is found. The code should then add 1 to this value. The user is them asked their name and this along with the new value is written to the original text file. so that the is a list of students with unique id number. The code works fine and add new students with sequential id number up to the number 10 but then stops and will only add the number 10. I suspect the int(max(list)+1 is the problem.
log=[];
Input=input("student or teacher?")
if Input =="student":
file=open("numbers.txt","r")
for line in file:
split=line.split(":")
log.append(split[0]);
file.close()
print(log)
else:
print()
number=int(max(log))+1
name=input("what is your name?")
file=open("numbers.txt","a")
file.write(number+":"+name+"\n")
file.close()

Related

Attempting to splice a recurring item out of a list

I have extracted files from an online database that consist of a roughly 100 titles. Associated with each of these titles is a DOI number, however, the DOI number is different for each title. To program this endeavor, I converted the contents of the website to a list. I then created for loop to iterate through each item of the list. What I want the program to do is iterate through the entire list and find where it says "DOI:" then to take the number which follows this. However, with the for loop I created, all it seems to do is print out the first DOI number, then terminates. How to I make the loop keep going once I have found the first one.
Here is the code:
resulttext = resulttext.split()
print(resulttext)
for item in resulttext:
if item == "DOI:":
DOI=resulttext[resulttext.index("DOI:")+1] #This parses out the DOI, then takes the item which follows it
print(DOI)

Assign user input as name for a list in python

This question may be a duplicate of "Convert User Input into a List Name". But what I am trying figure out is why does the last line of code not print the user's entry?
MyList = {}
UserListName = raw_input("Insert List Name")
MyList[UserListName]=[]
print "The user list name is ", MyList[UserListName]
It ends up printing "The user list name is []" though I am expecting "The user list name is JohnsList". If it read
print "The user list name is ", UserListName
wouldn't that just read the input only and not display it in list form?
Just figured out that the last line should read as follows to include user entry
print "The user list name is ", MyList

how to write simultaneously in a file while the program is still running

In simple words I have a file which contains duplicate numbers. I want to write unique numbers from the 1st file into a 2nd file. I have opened the 1st file in 'r' mode and the 2nd file in 'a+' mode. But it looks like that nothing is appended in the 2nd file while the program is running which gives wrong output. Any one can help me how do I fix this problem.
Thank you in advance.
This is my code
#!/usr/bin/env python
fp1 = open('tweet_mention_id.txt','r')
for ids in fp1:
ids = ids.rstrip()
ids = int(ids)
print 'ids= ',ids
print ids + 1
fp2 = open('unique_mention_ids.txt','a+')
for user in fp2:
user = user.rstrip()
user = int(user)
print user + 1
print 'user= ',user
if ids != user:
print 'is unique',ids
fp2.write(str(ids) + '\n')
break
else:
print 'is already present',ids
fp2.close()
fp1.close()
If unique_mention_ids.txt is initially empty, then you will never enter your inner loop, and nothing will get written. You should use the inner loop to determine whether or not the id needs to be added, but then do the addition (if warranted) outside the inner loop.
Similar logic applies for a non-empty file, but for a different reason: when you open it for appending, the file pointer is at the end of the file, and trying to read behaves as if the file were empty. You can start at the beginning of the file by issuing a fp2.seek(0) statement before the inner loop.
Either way: as written, you will write a given id from the first file for every entry in the second that it doesn't match, as opposed to it not matching any (which, given the file name, sounds like what you want). Worse, in the second case above, you will be over writing whatever came after the id that didn't match.

Python number averages using lists and keys

I'm working on a Python assignment and I'm totally stuck. Any assistance would be greatly appreciated. I know it's probably not as convoluted as it seems in my head... The details are below. Thanks very much.
Implement the following three functions (you should use an appropriate looping construct to compute the averages):
allNumAvg(numList) : takes a list of numbers and returns the average of all the numbers in the list.
posNumAvg(numList) : takes a list of numbers and returns the average of all the numbers in the list that are greater than zero.
nonPosAvg(numList) : takes a list of numbers and returns the average of all the numbers in the list that are less than or equal to zero.
Write a program that asks the user to enter some numbers (positives, negatives and zeros). Your program should NOT ask the user to enter a fixed number of numbers. Also it should NOT ask for the number of numbers the user wants to enter. But rather it should ask the user to enter a few numbers and end with -9999 (a sentinel value). The user can enter the numbers in any order. Your program should NOT ask the user to enter the positive and the negative numbers separately.
Your program then should create a list with the numbers entered (make sure NOT to include the sentinel value (-9999) in this list) and output the list and a dictionary with the following Key-Value pairs (using the input list and the above functions):
Key = 'AvgPositive' : Value = the average of all the positive numbers
Key = 'AvgNonPos' : Value = the average of all the non-positive numbers
Key = 'AvgAllNum' : Value = the average of all the numbers
Sample run:
Enter a number (-9999 to end): 4
Enter a number (-9999 to end): -3
Enter a number (-9999 to end): -15
Enter a number (-9999 to end): 0
Enter a number (-9999 to end): 10
Enter a number (-9999 to end): 22
Enter a number (-9999 to end): -9999
The list of all numbers entered is:
[4, -3, -15, 0, 10, 22]
The dictionary with averages is:
{'AvgPositive': 12.0, 'AvgNonPos': -6.0, 'AvgAllNum': 3.0}
EDIT: This is what I have so far, which I did pretty quick just to have a something to work with but I can't figure out how to implement the keys/dictionary like the assignment asks. Thanks again for any help.
print("This program takes user-given numbers and calculates the average")
counter = 0
sum_of_numbers = 0
first_question = int(input('Please enter a number. (Enter -9999 to end):'))
while first_question != -9999 :
ent_num = int(input('Please enter a number. (Enter -9999 to end):'))
sum_of_numbers = sum_of_numbers + ent_num
counter = counter + 1
first_question = int(input('Please enter a number (Enter -9999 to end):'))
print("Your average is " + str(sum_of_numbers/counter))
Welcome to Python programming, and programming in general!
From your code, I assume you are not entirely familiar with Python lists, dictionaries, and functions and how to use them. I'd suggest you look up tutorials for these; knowing how to use them will make your assignment much easier.
Here are some tutorials I found with some quick searches that might help:
Dictionary Tutorial,
List Tutorial,
Function Tutorial
When your assignment says to make three functions, you should probably make actual functions rather than trying to fit the functionality into your loop. For example, here is a simple function that takes in a number and adds 5 to it, then returns it:
def addFive(number):
return number + 5
To use it in your code, you would have something like this:
num = 6 # num is now 6
num = addFive(num) # num is now 11
So what you should do is create a list object containing all the numbers the user entered, and then pass that object into three separate functions - posNumAvg, nonPosAvg, allNumAvg.
Creating a dictionary of key-value pairs is pretty easy - first create the dictionary, then fill it with the appropriate values. For example, here is how I would create a dictionary like {'Hello': 'World'}
values = {}
values['Hello'] = 'World'
print(values) # Will print out {'Hello': 'World'}
So all you need to do is for each of the three values you need, assign the result of the function call to the appropriate key.
If this doesn't feel like quite enough for you to figure out this assignment, read the tutorials again and play with lists, dictionarys, and functions to try and get a feel for them. Good luck!
P.S. The append method of lists will be helpful to you. Try to figure out how to use it!

c++ searching and sorting in student list

What I want to do is to make a searching engine for searching student`s grade in student list with student number and student name. If multiple students match the requested name, the system will ask the user to enter the student id.
I have done the read txt file, however, I stopped at changing txt to array for storing and searching. I don't know how to store those data into array.
I got 20 students and here's two examples of student's grading in each subject:
SS6709 Peter VT001 C VT002 E VT003 D VT004 D VT009 A+ VY018 A++ VT024 B
SS9830 Amy VT001 D VT002 C VT003 C VT004 D VT009 D VT018 D VT023 B
think and define a class Item that will represent a single item that will hold the data about a single student (code, name, ...)
create an empty std::vector of those structures that will hold your "items"
open the file for reading
read the file line-by-line (use std::getline to get WHOLE lines, not just filestream.getline or file >> var operator)
(for each line that you read)
read the line as std::string, just as std::getline wants
create a Item for holding next group of data
analyze the line and cut/copy the student's code into myItem.code
analyze the line and cut/copy the student's name into myItem.name
analyze the line and cut/copy the XXXXXXXXXXXXXX into myItem.whateverelse
append the myItem that you have just read to the vector that you prepared earlier
close the file after reading all lines
Now you have a 'vector' that holds all the data, nicely stored as "items" that each have a code, name, and other data. You can now loop/search over that vector to find if any name or code matches.
it looks like you need to create your own data structure to store the values. You can define your own struct
struct Student{
string name
string iD
....
};
and use a array of type in this case Student
Student data_base[100];
this is a very basic approach but it should get you going...