Exporting variables to a Excel spreadsheet - python

I have made a program that calculates the population of greenflies. A task that I am struggling with is exporting several a series of variables to excel. I have worked out the math for the greenflies, but I can't figure out how to put that into a spreadsheet. Here's the code:
import winsound
import time
winsound.PlaySound("SystemHand", winsound.SND_ALIAS) #startup
#menu
print("Greenfly population model")
time.sleep(1)
one=1
print("1: Set the generation 0 values")
time.sleep(1)
two=2
print("2: Display the generation 0 values")
time.sleep(1)
three=3
print("3: Run the model")
time.sleep(1)
four=4
print("4: Export the data")
time.sleep(1)
five=5
print("5: Quit")
time.sleep(1)
#base code
while True: #wont shut down till option 5 is entered
print("")
ans=int(input("Please enter the number of the choice you want "))
if ans == one:
while True:
generations=float(input("Enter the number of generations you want the model to run for (Must be in 5 and 25 inclusive) "))
if generations < 5 or generations > 25:
print("Between 5 and 25 please")
else:
break
while True:
adultsur = float(input("Choose adult survival rate between 0 and 1 "))
if adultsur < 0 or adultsur > 1:
print ("Between 1 and 0 please") #wont mess up the decimals
else:
break
while True:
juvensur=float(input("Choose juvenile survivle rate between 0 and 1 "))
if juvensur < 0 or juvensur > 1:
print ("Between 1 and 0 please")
else:
break
while True:
sensur=float(input("Choose senile survivle rate between 0 and 1 "))
if sensur < 0 or sensur > 1:
print ("Between 1 and 0 please")
else:
break
juv=int(input("Choose the amount of juveniles "))
adu=int(input("Choose the amount of adults ")) #had issue with floats here so left it as int(input())
sen=int(input("Choose the amount of seniles "))
birth=int(input("Enter the birthrate of the adults "))
if ans == two:
print("The new generation to model is ",generations) #no issues here
time.sleep(1)
print("The adult survivul rate is ",adultsur)
time.sleep(1)
print("The juvenile survivul rate is ",juvensur)
time.sleep(1)
print("The senile survivul rate is ",sensur)
time.sleep(1)
print("There are ",juv," juveniles")
time.sleep(1)
print("There are ",adu," juveniles")
time.sleep(1)
print("There are ",sen," juveniles")
time.sleep(1)
print("The birthrate of adults is ",birth)
if ans == three:
print("Running module.")
time.sleep(0.1)
print("Running module..")
time.sleep(0.1)
print("Running module...")
time.sleep(0.1)
counter = 0
print("GENERATION JUVENILES ADULTS SENILES")
while generations > counter:
print ("",int(counter)," ", int(juv)," ", int(adu)," ", int(sen))
int(sen)
int(adu)#gets rid off the neverending decimals and rounds it up
int(juv)
sen *= sensur #takes out the old seniles
adu *= adultsur #does the math in a much simpler way than basic maths
juv *= len
juvn = juv
juv = adu * birth
sen += adu
adu += juvn
counter= counter+1 #will only repeat how many times the code was set to run
if ans == four
print ("",int(counter)," ", int(juv)," ", int(adu)," ", int(sen))
int(sen)
int(adu)
int(juv)
sen *= sensur
adu *= adultsur
juv *= len
juvn = juv
juv = adu * birth
sen += adu
adu += juvn
counter= counter+1
if ans == five:
print("Understandable, have a great day")
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
winsound.PlaySound("*", winsound.SND_ALIAS)
break
Can I have some help with exporting it or just teach me how to do it.
Thanks.

There are multiple ways that you can write data in excel and, additionally there are multiple formats that you can use.
Python csv module implements classes which can be used for writing and reading tabular data. For writing into file you can either use csv.writer or you can use csv.DictWriter.
The csv.writer returns a writer object which is responsible for converting the data into delimited strings. Here is an example on how to use it:
import csv
data_header = ['id', 'width', 'height']
data = [[0, 10, 5],
[1, 2, 3],
[2, 50, 40]]
with open('test.csv', 'w') as file_writer:
writer = csv.writer(file_writer)
writer.writerow(data_header)
for item in data:
writer.writerow(item)
The csv.DictWriter returns an object which operates like a regular writer but maps dictionaries onto output rows. Here is an example on how to use it:
import csv
data_header = ['id', 'width', 'height']
data = [{'id': 0, 'width': 10, 'height': 5},
{'id': 1, 'width': 2, 'height': 3},
{'id': 2, 'width': 50, 'height': 40}]
with open('test.csv', 'w') as file_writer:
dict_writer = csv.DictWriter(file_writer, data_header)
dict_writer.writeheader()
for item in data:
dict_writer.writerow(item)
As you can see, the difference between csv.writer and csv.DictWriter is how you provide the data to the interface. In csv.writer you provide the data in a list, and you are responsible for keeping the order in the list so it would be written in the correct columns and in csv.DictWriter you provide a dictionary where the keys are the column names and the value is the data for the column.

Related

How to modify this shortest-remaining-time-fisrt algorithm? (python lang)

The code has successfully able to compute same value of arrival times given by user input. However, its calculations are inaccurate. I do think the code messed up its computation in the initialisation part of priority queue. Please help me to modify it where it can accurately solve/compute with same value of arrival times as well as burst times.
Here is the code:
import sys
import pandas as pd
def srtf():
num_processes = []
# Get number of processes from user (between 5 and 10)
while True:
try:
num_processes = int(input("Enter the number of processes (minimum of 5 and maximum of 10): "))
if 5 <= num_processes <= 10:
break
else:
print("Invalid input. Number of processes must be between 5 and 10.")
except ValueError:
print("Invalid input. Only integers are allowed.")
print("\n")
# Get burst times and arrival times for each process
burst_times = []
for i in range(num_processes):
while True:
try:
burst_time = int(input("Enter burst time for process {} (minimum of 5 and maximum of 15): ".format(i+1)))
if 5 <= burst_time <= 15:
burst_times.append(burst_time)
break
else:
print("\nInvalid input. Burst time must be between 5 and 15.")
except ValueError:
print("\nInvalid input. Only integers are allowed.")
print("\n")
arrival_times = []
for i in range(num_processes):
while True:
try:
arrival_time = int(input("Enter arrival time for process {} (minimum of 0 and maximum of 12): ".format(i+1)))
if 0 <= arrival_time <= 12:
arrival_times.append(arrival_time)
break
else:
print("\nInvalid input. Arrival time must be between 0 and 12.")
except ValueError:
print("\nInvalid input. Only integers are allowed.")
# Initialize variables
process_id = [i+1 for i in range(num_processes)]
remaining_time = burst_times.copy()
completion_time = [0 for i in range(num_processes)]
waiting_time = [0 for i in range(num_processes)]
turnaround_time = [0 for i in range(num_processes)]
response_time = [0 for i in range(num_processes)]
current_time = 0
completed_processes = 0
import heapq
...
# Initialize the priority queue
priority_queue = []
...
while completed_processes != num_processes:
# Add all processes that have arrived to the priority queue
for i in range(num_processes):
if arrival_times[i] <= current_time and remaining_time[i] > 0:
heapq.heappush(priority_queue, (remaining_time[i], i))
if priority_queue:
remaining_time[priority_queue[0][1]] -= 1
current_time += 1
if response_time[priority_queue[0][1]] == 0: # first time process runs
response_time[priority_queue[0][1]] = current_time - arrival_times[priority_queue[0][1]]
if remaining_time[priority_queue[0][1]] == 0:
completed_processes += 1
completion_time[priority_queue[0][1]] = current_time
heapq.heappop(priority_queue)
else:
current_time += 1
# Calculate waiting time and turnaround time
for i in range(num_processes):
waiting_time[i]= completion_time[i] - burst_times[i] - arrival_times[i]
turnaround_time[i] = completion_time[i] - arrival_times[i]
# Calculate average waiting time, average turnaround time and average response time
avg_waiting_time = sum(waiting_time) / len(waiting_time)
avg_turnaround_time = sum(turnaround_time) / len(turnaround_time)
# Create a table with the results
results = {"Process ID": process_id, "Burst Time": burst_times, "Arrival Time": arrival_times,
"Completion Time": completion_time, "Waiting Time": waiting_time, "Turnaround Time": turnaround_time, "Response Time": response_time}
df = pd.DataFrame(results)
# Print the table
print("\n", df)
# Print the average turnaround time, waiting time and response time
print("\n")
print("Average Turnaround Time: ", "{:.2f}".format(avg_turnaround_time))
print("Average Waiting Time: ", "{:.2f}".format(avg_waiting_time))
return df, avg_turnaround_time, avg_waiting_time
srtf()
This is an example of the output(inaccurate) from the terminal:

Lottery win predictor

I am a beginner in Python and I think I need some help with my program. Any kind of help or advice would be appreciated:)
You can see the program below, when I run it it gets stuck on the part of comparing the random ticket with the winning ticket(win_combination).
from random import choice
#Winning ticket
win_combination = input("Enter the winning combination of 4 numbers(1-digit-numbers): ")
while len(win_combination) != 4:
if len(win_combination) > 4:
win_combination = input("Reenter a shorter combination(4 one-digit-numbers): ")
elif len(win_combination) < 4:
win_combination = input("Reenter a longer combination(4 one-digit-numbers): ")
print(f"Winning combination is {win_combination}.")
#Specifying range of numbers to choose from
range = range(0, 10)
#Making a fake comparison-ticket to start of the loop
random_ticket = [0, 0]
random_ticket_string = f"{random_ticket[0]}{random_ticket[1]}{random_ticket[2]}{random_ticket[3]}"
#Params for the loop
n_tries = 0
n_guesses = 1
while random_ticket_string != win_combination:
while n_tries > 4:
random_ticket.clear()
number = choice(range)
random_ticket.append(number)
n_tries += 1
n_guesses += 1
random_ticket_string = f"{random_ticket[0]}{random_ticket[1]}"
if random_ticket_string == win_combination:
chance_to_win = f"{(1 / n_guesses) * 100}%"
print("Estimated percent to win is " + chance_to_win + ", it took " + f"{n_guesses} to match the winning combination.")
else:
n_tries = 0

Python Credit Card Validation

I'm a beginner Python learner and I'm currently working on Luhn Algorithm to check credit card validation. I wrote most of the code, but I'm stuck with 2 errors I get 1st one is num is referenced before assignment. 2nd one I'm getting is object of type '_io.TextIOWrapper' has no len(). Further help/ guidance will be greatly appreciated.
These are the steps for Luhn Algorithm (Mod10 Check)
Double every second digit from right to left. If this “doubling” results in a two-digit number, add the two-digit
number to get a single digit.
Now add all single digit numbers from step 1.
Add all digits in the odd places from right to left in the credit card number.
Sum the results from steps 2 & 3.
If the result from step 4 is divisible by 10, the card number is valid; otherwise, it is invalid.
Here's what my output is supposed to be
Card Number Valid / Invalid
--------------------------------------
3710293 Invalid
5190990281925290 Invalid
3716820019271998 Valid
37168200192719989 Invalid
8102966371298364 Invalid
6823119834248189 Valid
And here is the code.
def checkSecondDigits(num):
length = len(num)
sum = 0
for i in range(length-2,-1,-2):
number = eval(num[i])
number = number * 2
if number > 9:
strNumber = str(number)
number = eval(strNumber[0]) + eval(strNumber[1])
sum += number
return sum
def odd_digits(num):
length = len(num)
sumOdd = 0
for i in range(length-1,-1,-2):
num += eval(num[i])
return sumOdd
def c_length(num):
length = len(num)
if num >= 13 and num <= 16:
if num [0] == "4" or num [0] == "5" or num [0] == "6" or (num [0] == "3" and num [1] == "7"):
return True
else:
return False
def main():
filename = input("What is the name of your input file? ")
infile= open(filename,"r")
cc = (infile.readline().strip())
print(format("Card Number", "20s"), ("Valid / Invalid"))
print("------------------------------------")
while cc!= "EXIT":
even = checkSecondDigits(num)
odd = odd_digits(num)
c_len = c_length(num)
tot = even + odd
if c_len == True and tot % 10 == 0:
print(format(cc, "20s"), format("Valid", "20s"))
else:
print(format(cc, "20s"), format("Invalid", "20s"))
num = (infile.readline().strip())
main()
You just forgot to initialize num
def main():
filename = input("What is the name of your input file? ")
infile= open(filename,"r")
# initialize num here
num = cc = (infile.readline().strip())
print(format("Card Number", "20s"), ("Valid / Invalid"))
print("------------------------------------")
while cc!= "EXIT":
even = checkSecondDigits(num)
odd = odd_digits(num)
c_len = c_length(num)
tot = even + odd
if c_len == True and tot % 10 == 0:
print(format(cc, "20s"), format("Valid", "20s"))
else:
print(format(cc, "20s"), format("Invalid", "20s"))
num = cc = (infile.readline().strip())
First, maybe you should remove the extra characters:
def format_card(card_num):
"""
Formats card numbers to remove any spaces, unnecessary characters, etc
Input: Card number, integer or string
Output: Correctly formatted card number, string
"""
import re
card_num = str(card_num)
# Regex to remove any nondigit characters
return re.sub(r"\D", "", card_num)
After check if credit card is valid using the Luhn algorithm:
def validate_card(formated_card_num):
"""
Input: Card number, integer or string
Output: Valid?, boolean
"""
double = 0
total = 0
digits = str(card_num)
for i in range(len(digits) - 1, -1, -1):
for c in str((double + 1) * int(digits[i])):
total += int(c)
double = (double + 1) % 2
return (total % 10) == 0
This is a very simpler version of code it is based on lunh's algorithm
def validator(n):
validatelist=[]
for i in n:
validatelist.append(int(i))
for i in range(0,len(n),2):
validatelist[i] = validatelist[i]*2
if validatelist[i] >= 10:
validatelist[i] = validatelist[i]//10 + validatelist[i]%10
if sum(validatelist)%10 == 0:
print('This a valid credit card')
else:
print('This is not valid credit card')
def cardnumber():
result=''
while True:
try:
result = input('Please enter the 16 digit credit card number : ')
if not (len(result) == 16) or not type(int(result) == int) :
raise Exception
except Exception:
print('That is not a proper credit card number. \nMake sure you are entering digits not characters and all the 16 digits.')
continue
else:
break
return result
def goagain():
return input('Do you want to check again? (Yes/No) : ').lower()[0] == 'y'
def main():
while True:
result = cardnumber()
validator(result)
if not goagain():
break
if __name__ == '__main__':
main()
Old thread but the answer concerns me... and the real issue wasn't identified.
Actually, the error is that you have used the identifier (num) for the parameter when defining checkSecondDigits as the identifier/name of the argument when calling the function in the mainline. The function should be called in main() by
even = checkSecondDigits(cc) so the value in cc (which is the argument) is passed into num (as the parameter) for use within the function.
The same rookie error is made with odd_digits and cc_length.
This question (and the initially suggested answer) demonstrates a fundamental mis-understanding of passing arguments to parameters...
The suggested 'declaring' of num just hides this error/misunderstanding and also obfuscates the local and global scopes of num (which should only be local) and cc (which is global) so whilst the suggestion works in this case, it works for the wrong reason and is poor style and bad programming.
Further,
num should not appear anywhere in main() as it should be local to (only appear inside of) the functions called...
The last line in this code should be the same as the first, but the last line incorrectly assigns the data to num instead of cc
cc = (infile.readline().strip())
print(format("Card Number", "20s"), ("Valid / Invalid"))
print("------------------------------------")
while cc!= "EXIT":
even = checkSecondDigits(num)
odd = odd_digits(num)
c_len = c_length(num)
tot = even + odd
if c_len == True and tot % 10 == 0:
print(format(cc, "20s"), format("Valid", "20s"))
else:
print(format(cc, "20s"), format("Invalid", "20s"))
num = (infile.readline().strip())
you can use my code for card validation it is 100% dynamic because of the card structure is stored in CSV file, so it is easy to update here is the code on GitHub profile, python file link, code explanation file link and CSV for datafile link
python code:
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 10 20:55:30 2019
#author: Preyash2047#gmail.com
"""
import csv
import numpy as np
#csv file imported and storf in reader
reader = csv.DictReader(open("card_data.csv"))
#input card number
card_number = input("Enter the card No: ")
#global variable declaration
min_digits=0
max_digits=0
card_number_list = list(card_number)
card_number_list_reverse=card_number_list[::-1]
card_number_length=len(card_number_list)
first_digit = int(card_number_list[0])
#global variable for final output
card_provider_list_number = 0
result_found = False
card_number_digits = 0
mit_name=""
#list
start=[]
end=[]
name=[]
c_d=[]
number_length=[]
min_max_digits_list=[]
#append the list from csv
for raw in reader:
start.append(raw['start'])
end.append(raw['end'])
name.append(raw['name'])
c_d.append(raw['c_d'])
number_length.append(raw['number_length'])
#initialize the value of min_digits & max_digits
def min_max_digits():
global min_digits
global max_digits
for i in range(len(start)):
available_length=number_length[i].split(',')
for j in range(len(available_length)):
min_max_digits_list.append(available_length[j])
min_max_digits_array = np.array(min_max_digits_list)
np.unique(min_max_digits_array)
min_digits=int(min(min_max_digits_array))
max_digits=int(max(min_max_digits_array))
#list to int
def list_to_int(noofdigits):
str1 = ""
return int(str1.join(noofdigits))
#card validation
def iin_identifier():
first_six_digit = list_to_int(card_number_list[0:6])
for i in range(len(start)):
if(first_six_digit >= int(start[i]) and first_six_digit <= int(end[i])):
available_length=number_length[i].split(',')
for j in range(len(available_length)):
if(card_number_length == int(available_length[j])):
global card_provider_list_number
card_provider_list_number = i
global card_number_digits
card_number_digits = available_length[j]
global result_found
result_found = True
#Major Industry Identifier (MII) identification
def mit_identifier():
global first_digit
global mit_name
switcher = {
1: "Airlines",
2: "Airlines",
3: "Travel and Entertainment",
4: "Banking and Financial Services",
5: "Banking and Financial Services",
6: "Merchandising and Banking",
7: "Petroleum",
8: "Health care, Telecommunications",
9: "National Assignment"
}
mit_name=switcher.get(first_digit, "MIT Identifier Not Found")
#Luhn Algorithm or modulo-10 Algorithm
def luhn_algorithm():
for i in range(card_number_length):
if(i%2!=0 and i!=0):
card_number_list_reverse[i]=int(card_number_list_reverse[i])*2
#print(str(i)+" "+ str(card_number_list_reverse[i]))
if(len(str(card_number_list_reverse[i]))==2):
even_number_2=list(str(card_number_list_reverse[i]))
card_number_list_reverse[i] = int(even_number_2[0])+int(even_number_2[1])
#print("\tsubsum "+str(i)+" "+str(card_number_list_reverse[i]))
else:
card_number_list_reverse[i]=int(card_number_list_reverse[i])
division_int = int(sum(card_number_list_reverse)/10)
division_float=sum(card_number_list_reverse)/10
if(division_int-division_float==0):
return True
#initial level number length validation
def card_number_validation():
min_max_digits()
if(card_number_length>= min_digits and card_number_length <= max_digits and first_digit != 0):
iin_identifier()
mit_identifier()
if(result_found and luhn_algorithm()):
print("\nEntered Details are Correct\n")
print("\nHere are the some details we know about you card")
print("\nNo: "+card_number)
print("\nIssuing Network: "+name[card_provider_list_number])
print("\nType: "+c_d[card_provider_list_number]+" Card")
print("\nCategory of the entity which issued the Card: "+mit_name)
else:
print("\nCard Number is Invalid\nPlease renter the number!\n")
else:
print("\nCard Number is Invalid\n")
#method called to run program
card_number_validation()
n = input("Enter 16-digit Credit Card Number:")
lst = []
for i in range(16):
lst.append(n[i])
# print(lst)
# list1 = n.split()
# print(list1)
def validate_credit_card():
global lst
if len(lst) == 16:
for i in range(0, len(lst)):
lst[i] = int(lst[i])
# print(lst)
last = lst[15]
first = lst[:15]
# print(first)
# print(last)
first = first[::-1]
# print(first)
for i in range(len(first)):
if i % 2 == 0:
first[i] = first[i] * 2
if first[i] > 9:
first[i] -= 9
sum_all = sum(first)
# print(first)
# print(sum_all)
t1 = sum_all % 10
t2 = t1 + last
if t2 % 10 is 0:
print("Valid Credit Card")
else:
print("Invalid Credit Card!")
else:
print("Credit Card number limit Exceeded!!!!")
exit()
if __name__ == "__main__":
validate_credit_card()

Why is Filename not a db that the shelve module can open?

import random
import sys
import shelve
def get_input_or_quit(prompt, quit="Q"):
prompt += " (Press '{}' to exit) : ".format(quit)
val = input(prompt).strip()
if val.upper() == quit:
sys.exit("Goodbye")
return val
def prompt_bool(prompt):
while True:
val = get_input_or_quit(prompt).lower()
if val == 'yes':
return True
elif val == 'no':
return False
else:
print ("Invalid input '{}', please try again".format(val))
def prompt_int_small(prompt='', choices=(1,2)):
while True:
val = get_input_or_quit(prompt)
try:
val = int(val)
if choices and val not in choices:
raise ValueError("{} is not in {}".format(val, choices))
return val
except (TypeError, ValueError) as e:
print(
"Not a valid number ({}), please try again".format(e)
)
def prompt_int_big(prompt='', choices=(1,2,3)):
while True:
val = get_input_or_quit(prompt)
try:
val = int(val)
if choices and val not in choices:
raise ValueError("{} is not in {}".format(val, choices))
return val
except (TypeError, ValueError) as e:
print(
"Not a valid number ({}), please try again".format(e)
)
role = prompt_int_small("Are you a teacher or student? Press 1 if you are a student or 2 if you are a teacher")
if role == 1:
score=0
name=input("What is your name?")
print ("Alright",name,"welcome to your maths quiz."
" Remember to round all answers to 5 decimal places.")
level_of_difficulty = prompt_int_big("What level of difficulty are you working at?\n"
"Press 1 for low, 2 for intermediate "
"or 3 for high\n")
if level_of_difficulty == 3:
ops = ['+', '-', '*', '/']
else:
ops = ['+', '-', '*']
for question_num in range(1, 11):
if level_of_difficulty == 1:
max_number = 10
else:
max_number = 20
number_1 = random.randrange(1, max_number)
number_2 = random.randrange(1, max_number)
operation = random.choice(ops)
maths = round(eval(str(number_1) + operation + str(number_2)),5)
print('\nQuestion number: {}'.format(question_num))
print ("The question is",number_1,operation,number_2)
answer = float(input("What is your answer: "))
if answer == maths:
print("Correct")
score = score + 1
else:
print ("Incorrect. The actual answer is",maths)
if score >5:
print("Well done you scored",score,"out of 10")
else:
print("Unfortunately you only scored",score,"out of 10. Better luck next time")
class_number = prompt_int_big("Before your score is saved ,are you in class 1, 2 or 3? Press the matching number")
filename = (str(class_number) + "txt")
with shelve.open(filename) as db: # This is the effected line
old_scores = db.get(name, [])
old_scores.extend(score)
db[name] =score[-3:]
if prompt_bool("Do you wish to view previous results for your class"):
for line in lines:
print (line)
else:
sys.exit("Thanks for taking part in the quiz, your teacher should discuss your score with you later")
if role == 2:
class_number = prompt_int_big("Which class' scores would you like to see? Press 1 for class 1, 2 for class 2 or 3 for class 3")
filename = (str(class_number) + "txt")
sort_or_not = int(input("Would youlike to sort these scores in any way? Press 1 if the answer is no or 2 if the answer is yes"))
if sort_or_not == 1:
f = open(filename, "r")
lines = [line for line in f if line.strip()]
lines.sort()
for line in lines:
print (line)
if sort_or_not == 2:
type_of_sort = int(input("How would you like to sort these scores? Press 1 for scores in alphabetical order with each student's highest score for the tests, 2 if you would like to see the students' highest scores sorted from highest to lowest and 3 if you like to see these student's average scores sorted from highest to lowest"))
if type_of_sort == 1:
with open(filename , 'r') as r:
for line in sorted(r):
print(line, end='')
if type_of_sort == 2:
file = open(filename, 'r')
lines = file.read().splitlines()
s = {lines[i]:[int(k) for k in lines[i+1:i+4]] for i in range(0,len(lines),4)}
for i in sorted(s.keys()):
print (i,max(s[i]))
avg_mark = lambda name:sum(s[name])/len(s[name])
for i in sorted(s.keys(),key=avg_mark,reverse=True):
print (i,avg_mark(i))
if type_of_sort == 3:
file = open(filename, 'r')
lines = file.read().splitlines()
s = {lines[i]:[int(k) for k in lines[i+1:i+4]] for i in range(0,len(lines),4)}
for i in sorted(s.keys()):
print (i,max(s[i]))
high_mark = lambda name:max(s[name])
for i in sorted(s.keys(),key=high_mark,reverse=True):
print (i,high_mark(i))
The shelve module part of my code is not working and I don't understand what the error is trying to say. I am trying to save the last three scores to a students name and then sort it in a variety of ways . The error comes up after the quiz is completed as a student.
Try deleting the file at filename and running the script again, so that the script is responsible for creating the database file in the correct format. If you ask it to open an empty file or a file that was created by some other means (e.g. CSV, JSON, etc.) then it doesn't know how to handle it.

How can i speed up this python code?

So I recently coded this as a little challenge to see how quick I could do it. Now since its working an such I want to speed it up. It finds all the proper devisors of a number, the highest proper devisor and times how long it all takes. The problem is with number like 5000 it takes 0.05 secs but with numbers like 99999999999 it takes 1567.98 secs.
this the old code I have made a new and improved version below
import time
def clearfile(name):
file = open(name + ".txt", "r")
filedata = file.read()
file.close()
text_file = open(name + ".txt", "w")
text_file.write("")
text_file.close()
def start():
num = input("Enter your Number: ")
check(num)
def check(num):
try:
intnum = int(num)
except ValueError:
error(error = "NON VALID NUMBER")
if(intnum < 0):
error(error = "POSITIVE NUMBERS ONLY")
else:
finddivisor(intnum)
def finddivisor(intnum):
starttimer = time.time()
i = 1
print("\nThe divisors of your number are:"),
while i <= intnum:
if (intnum % i) == 0:
print(i)
file = open("numbers.txt", "r")
filedata = file.read()
file.close()
text_file = open("numbers.txt", "w")
text_file.write(str(i) +"\n"+ filedata)
text_file.close()
i += 1
properdivisor(starttimer)
def properdivisor(starttimer):
file = open("numbers.txt", "r")
highest = file.readlines()
print("\nThe Highest Proper Divisor Is\n--------\n" + highest[1] + "--------" + "\nIt took" ,round(time.time() - starttimer, 2) ,"seconds to finish finding the divisors.\n")
restart(errorrestart = "false")
def restart(errorrestart):
if errorrestart == "false":
input("Do You Want Restart?\nPress Enter To Restart Or Close The Programe To Leave")
start()
elif errorrestart == "true":
input("\nThere Was An Error Detected.\nPress Enter To Restart Or Close The Programe To Leave")
start()
def error(error):
print("\n----------------------------------\nERROR - " + error + "\n----------------------------------")
restart(errorrestart = "true")
clearfile(name = "numbers")
start()
Can someone speed it up for me
EDIT 1
so after looking over it I have now edited it to be moving it away from a file to an array
import time
from array import *
def programme():
num = input("Enter your Number: ")
try:
intnum = int(num)
except ValueError:
error("NOT VALID NUMBER")
if(intnum < 0):
error("POSITIVE NUMBERS ONLY")
else:
numbers = array("i",[])
starttimer = time.time()
i = 1
print("\nThe divisors of your number are:"),
while i <= intnum:
if (intnum % i) == 0:
numbers.insert(0,i)
print(i)
i += 1
print("\nThe Highest Proper Divisor Is\n--------\n" + str(numbers[1]) + "\n--------" + "\n\nIt took" ,round(time.time() - starttimer, 2) ,"seconds to finish finding the divisors.\n")
def error(error):
print("\n----------------------------------\nERROR - " + error + "\n----------------------------------\n")
running = True
while(running == True):
programme()
print("----------------------------------")
restart = input("Do You Want Restart?")
restart = restart.lower()
if restart in ("yes", "y", "ok", "sure", ""):
print("Restarting\n----------------------------------")
else:
print("closing Down")
running = False
New Edit
import time, math
from array import *
def programme():
num = input("Enter your Number: ")
try:
intnum = int(num)
if(intnum < 0):
error("POSITIVE NUMBERS ONLY")
else:
numbers = array("i",[])
starttimer = time.time()
i = 1
print("\nThe divisors of your number are:"),
while i <= math.sqrt(intnum):
if (intnum % i) == 0:
numbers.insert(0,i)
numbers.insert(0,int(intnum/i))
print(i,":", int(intnum/i))
i += 1
numbers = sorted(numbers, reverse = True)
print("The Highest Proper Divisor Is\n--------\n",str(numbers[1]) , "\n--------\nIt took" ,round(time.time() - starttimer, 2) ,"seconds to finish finding the divisors." )
except ValueError:
error("NOT VALID NUMBER")
except OverflowError:
error("NUMBER IS TO LARGE")
except:
error("UNKNOWN ERROR")
def error(error):
print("\n----------------------------------\nERROR - " + error + "\n----------------------------------\n")
running = True
while(running):
programme()
print("----------------------------------")
restart = input("Do You Want Restart?")
restart = restart.lower()
if restart in ("yes", "y", "ok", "sure", ""):
print("Restarting\n----------------------------------")
else:
print("closing Down")
running = False
If you have divisor a of number n then you can tell one more divisor of n b = n / a. Moreover if a <= sqrt(n) then b >= sqrt(n) and vice versa. It means in your finddivisor function you can iterate while i * i <= n and print both divisors i and n / i.
By the way, you shouldn't open, read and close files in cycle. Open it once before cycle and close after if you need to read/write several times.
You don't need to read and rewrite the entire file every time you want to put a single entry into it. You can just do it once when you know what change you want. Also you could just append to it. Maybe something like this:
def finddivisor(intnum):
starttimer = time.time()
print("\nThe divisors of your number are:")
divs = set()
for i in range(1, int(math.sqrt(intnum)) +1):
if intnum % i == 0:
print(i)
divs.add(i)
divs.add(intnum // i)
with open("numbers.txt", "a") as file:
file.writelines("{}\n".format(ln) for ln in sorted(divs, reverse=True))
also your program flow is building a very deep stack. Try and flatten it with something like
def start():
clearfile()
while True:
n = get_number()
starttimer = time.time()
finddivisor(n)
properdivisor(starttimer)
input("Press Enter To Restart Or Close The Programe To Leave")
in properdivisor you dont need to read the whole file either, you just need the first line. so maybe something like:
def properdivisor(starttimer):
with open(FILENAME, "r") as file:
highest = file.readline().strip()
print "\nThe Highest Proper Divisor Is"
print "--------%s--------" % highest
print "It took %0.2f seconds to finish finding the divisors.\n" % (time.time() - starttimer)
AFTER EDIT
Something like this is how I would do it, and it runs less then a second on my box:
import math
def get_divisors(n):
yield 1
sqroot = int(math.sqrt(n))
for x in xrange(2, sqroot):
if n % x == 0:
yield x
yield n / x
if sqroot**2 == n:
yield sqroot
divisors = sorted(get_divisors(999999999999))
print divisors

Categories