You need to call the function "input"
So it'd look like:
input()
You need to call the function "input"
So it'd look like:
input()
Hmm... Perhaps use a dictionary to sort it all?
# Original values
L1=['v1', 'v2', 'v3', 'v4', 'v5']
L2= [3, 1, 7, 6, 5]
# Dictionary to keep them in order
d = {}
for x in range(0, len(L2)):
d[L2[x]] = L1[x]
# The dictionary should automatically sort by number, so your output of the dictionary is:
#{1: 'v2', 3: 'v1', 5: 'v5', 6: 'v4', 7: 'v3'}
# Now, create two new lists to store each value if you want
L11 = []
L21 = []
for x in d.items():
L11.append(x[0])
L22.append(x[1])
Hey, just to show you something dangerous you can do with input(), (If the module 'sys' has been imported):
>>> input("> ")
> sys.stdout.write('OGGA')
OGGA
I know it's not much, but think of things you could do with that
I'm still using 2.6, but I have the newest 3 installed. I don't use 3, because there aren't many modules made for it. However, I do try and use 3's syntax in 2.6 where applicable (ie print("") not print ""). I do wish that I could do one liner "for" statements with Python, but I guess that'll have to wait xP
Well, you could just do this as well:
import time
timevar1 = time.time()
# YOUR CODE GOES HERE
timevar2 = time.time()
print(timevar2-timevar1)
That will determine how many seconds it takes for your script to execute
The escape character "\t" for tabs works wonders ;)
Okay, first off... See all of those purple words now that it's in the [-code-] tags? Yeah, those mean that they are internally defined, and do something. You want to change all of those variables to something else.
Second, your error is self-explanatory, the variable "line" is not defined anywhere in the program.
Here's a good working program:
# PURPOSE: to count the number of words, lines, and characters in a file
#
# INPUT(S): a file name to be used
#
# OUTPUT(S): count for number of lines, words, and characters in the file
#
# EXAMPLES: input: ; output:
# input: ; output:
#
################################################################################
def fileCount(f):
# Define all variables required
linecount = 0
wordcount = 0
charcount = 0
words = []
# Open the file
_file = open(f, 'r')
# Loop through each line
for line in _file:
linecount += 1
word = line.split()
words += word
# Loop through all words found
for word in words:
wordcount += 1
# Loop through each word for characters
for char in word:
charcount += 1
return (linecount, wordcount, charcount)
# Name of the file
fname = raw_input('Enter the name of the file to be used: ')
# Unpack the tuple returned by the fileCount
lineCount, charCount, wordCount = fileCount(fname)
print "There are", lineCount, "lines in the file."
print "There are", charCount, "characters in the file."
print "There are", wordCount, "words in the file."
EDIT: Didn't notice your post when I quick posted xD
Dude, what's up that? It shouldn't be returning '0' for hex(ord('\x00')). It will return '0x00'.
EDIT:
After relooking at the code snippet on my computer, I realize that it does not return what I thought. xD It does indeed return '0x0'. However, you can just do a check of len of each 's'. :D
if len(s) == 1: s = '0'+s
So, here's the whole code:
def tohex(s):
hexed = ''
for x in s:
a = ord(hex(x)).replace('0x', '')
if len(a) == 1: a = '0' + a
a += ' ' #add spaces between hex (looks like an editor)
hexed += a
a = a[:-1] # Get rid of the last space
return hexed
Ah, you misunderstand the code. You call the function, and it will return a string object that you can write. Thus, you can do this:
e = tohex('Hello world')
'''e = '48656c6c6f20776f726c64''''
And then whatever you need to do, in order to display a text string in your GUI (Tkinter, I presume? I have no experience with Tkinter, so I cannot help with that). Ah, and perhaps something to speed up your read times?
Perhaps you can read in only 5~10 lines at a time, and hex that, then do that for the whole file. That should help it out. That's what I usually do
Ah buddy... Okay, let me show you some hints on hex values. Your code has confused me beyond belief xDDD
Okay, here are some basic functions you're going to have to get comfortable using:
int(x [, base])
hex(x)
ord(c)
chr(i)
str.replace(old, new)
Okay, here's how we can use this information. Say we have this string here: 'Hello world'
Now, we want to convert this to a hex value, correct? Also, we don't want a nasty string like this: '0x480x650x6c0x6c0x6f0x200x770x6f0x720x6c0x64'
Kinda hard to read, right?
Okay, here's how we convert that string into a readable 2 digit hex code
# First off, some background:
# ord(c) will return the ascii value of a character
# That should help in understanding this
# Get rid of the print statements if you want
def tohex(s):
hexed = ''
for x in s:
a = ord(x)
print(a)
a = hex(a)
print(a)
a = a.replace('0x', '') # Get rid of that ugly '0x'
print(a)
hexed += a
return hexed
tohex('Hello world')
Okay, so now we have a hex representation of that string, right? Well, now you can do your hex editing if needed be. But, we want to go ahead and reconvert that back into a string, right?
# More background:
# chr(i) returns a character based on the number you type
# int(x [, base]) can take two arguements
# Normally, you just want something like int('9') to return an int 9
# However, we are working with a …
Er, what have you managed to do so far? Can you convert strings into hex strings? Such as a hex view of 'Hello world!!!'? Here is the hex values for it if you don't know: '48656c6c6f20776f726c64212121'
Good luck :D
Post if you need help
Also, do a google seach for Tkinter python manual. That'll help
I bet he's making a game which takes place on a different planet :D
# Hashes/Arrays are known as Libraries in Python:
a = {}
a['b'] = 0
print(a)
print(a['b'])
Change events = list(events[0] to events = list(events).split('\n'). Then make a loop to iterate through each events listing, then put the original loop inside of that. Please try and figure this one out before asking for help. You will have to show me all the tries you have done before expecting me to help with your homework anymore. No offense, but you should be trying to figure it out on your own. That should be a requirement of being a programmer: able to figure out why something works on your own by modifying what you are working on
elif menu_choice == 3:
filename = raw_input("Filename to load: ")
pickle_file = open(filename, "r")
events = cPickle.load(pickle_file)
pickle_file.close()
events = list(events[0])
tabs = ""
for x in events[:-1]:
tabs += x + "\t"
tabs += events[-1]
print "Events:" + tabs
import cPickle
def print_menu():
print '1. Add an Event'
print '2. Save Events to File'
print '3. Load Events from File'
print '4. Quit'
print()
event_list = []
menu_choice = 0
print_menu()
while True:
menu_choice = input("Select Menu Item (1-4): ")
if menu_choice == 1:
print "Add Event"
date = raw_input("Date: ")
# yyyy/mm/dd
# Assuming that the user inputs this: 2010, 3, 13
date = date.split(', ')
if len(date[0]) == 2:
date[0] = '20'+date[0]
if len(date[1]) == 1:
date[1] = '0'+date[1]
if len(date[2]) == 1:
date[2] = '0'+date[2]
date = '/'.join(date)
#
time = raw_input("Time: ")
title = raw_input("Title: ")
desc = raw_input("Description: ")
loc = raw_input("Location: ")
attend = raw_input("Attendee(s): ")
#ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend
# Add a newline to seperate each entry
ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend + '\n'
event_list.append(ui)
elif menu_choice == 2:
filename = raw_input("Filename to save: ")
pickle_file = open(filename, "w")
cPickle.dump(event_list, pickle_file)
pickle_file.close()
elif menu_choice == 3:
filename = raw_input("Filename to load: ")
pickle_file = open(filename, "r")
events = cPickle.load(pickle_file)
pickle_file.close()
events = list(events[0])
print "Events:"
for x in events:
print " "+x
elif menu_choice == 4:
break
else:
print_menu()
print "Goodbye"
My Output:
1. Add an Event
2. Save Events to File
3. Load Events from File …
Okay, sorry. The tuple appears to not have a .split() function, only a list does. So, we shall go ahead and convert the tuple to a list, then iterate through the split:
n = []
for x in events:
n.append(x)
# Make the tuple to a list
events = n[:]
# NOTE:
# You could just do a conversion using the list() function;
# IE. events = list(events)
# Less typing, but the other way you'll understand how the tuple and list works
# Separate the events at the '\n' marker
for x in events[0].split('\n'):
# Iterate through the list of each entry
for y in x:
# print the result
print y
# Separate the events at the '\n' marker
for x in events[0].split('\n'):
# Iterate through the list of each entry
for y in x:
# print the result
print y
Had to get rid of the commented part of code (First block), then change date = input() to date = raw_input(). You still have to change the way the event load presents itself. But I fixed the major part for you:
import cPickle
def print_menu():
print '1. Add an Event'
print '2. Save Events to File'
print '3. Load Events from File'
print '4. Quit'
print()
event_list = []
menu_choice = 0
print_menu()
while True:
menu_choice = input("Select Menu Item (1-4): ")
if menu_choice == 1:
print "Add Event"
date = raw_input("Date: ")
# yyyy/mm/dd
# Assuming that the user inputs this: 2010, 3, 13
date = date.split(', ')
if len(date[0]) == 2:
date[0] = '20'+date[0]
if len(date[1]) == 1:
date[1] = '0'+date[1]
if len(date[2]) == 1:
date[2] = '0'+date[2]
date = '/'.join(date)
#
time = raw_input("Time: ")
title = raw_input("Title: ")
desc = raw_input("Description: ")
loc = raw_input("Location: ")
attend = raw_input("Attendee(s): ")
#ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend
# Add a newline to seperate each entry
ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend + '\n'
event_list.append(ui)
elif menu_choice == 2:
filename = raw_input("Filename to save: ")
pickle_file = open(filename, "w")
cPickle.dump(event_list, pickle_file)
pickle_file.close()
elif menu_choice == 3:
filename = raw_input("Filename to load: ")
pickle_file = open(filename, "r")
events …
Here you go. I still don't know how the user will be inputting data, so I included two choices for you. Just get rid of the one you don't want. I was half contemplating on getting rid of whitespace to make you tab each one like you did, but I left it in to be nice =]
import cPickle
def print_menu():
print '1. Add an Event'
print '2. Save Events to File'
print '3. Load Events from File'
print '4. Quit'
print()
event_list = []
menu_choice = 0
print_menu()
while True:
menu_choice = input("Select Menu Item (1-4): ")
if menu_choice == 1:
print "Add Event"
date = input("Date: ")
# yyyy/mm/dd
# Assuming that the user inputs this: 20100313
date = date[0:4]+"/"+date[4:6]+"/"+date[6:8]
# Assuming that the user inputs this: 2010, 3, 13
date = date.split(', ')
if len(date[0]) == 2:
date[0] = '20'+date[0]
if len(date[1]) == 1:
date[1] = '0'+date[1]
if len(date[2]) == 1:
date[2] = '0'+date[2]
date = '/'.join(date)
#
time = raw_input("Time: ")
title = raw_input("Title: ")
desc = raw_input("Description: ")
loc = raw_input("Location: ")
attend = raw_input("Attendee(s): ")
#ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend
# Add a newline to seperate each entry
ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend + '\n'
event_list.append(ui)
elif menu_choice == 2:
filename …
Your code looks like it's running fine to me. Would you be able to edit your post and put
tags around the code? It's hard to read. I also don't know what exactly you want. It's definitely taking the input how you want it. Perhaps just ask the user to put it in the format dd/mm/yy?
I believe that whole module was deprecated as of version 2 or so. I think anyways.
@ Sahiti:
Please learn netiquette and some english, not "eubonics", or "txtspeak" on online forums. While we will help you, we would like if you spoke as you would in a normal conversation. Also, if you need help with the 'C' syntax/language, please ask in the appropriate forum.
Thank you :D
um, here's what you do.
On your pc, make a file in your documents that is named something like:
runpython.pythonscript
It doesn't matter what it is, just so long as no one else will have it. Then, make a script that checks for that file in your documents, and if it's there, run the script on your flash drive
If not, then do nothing
Then, compile it using py2exe, and put it on your flash drive, along with the script, and the autorun.inf file.
That's how I'd do it
Here's what I think:
If it refrences outside objects at all, than it will be different if you have it on a different server. Check and make sure that all the folders and files are completely the same.
Well, if you want it to be:
'(1.0+nfpermult+1.0)'
Then it appears that 'nf' will be at both the beginning and end, so I'll assume that the original is:
'nf+nfpermult+nf'
so, you do this:
a = 'nf+nfpermult+nf'
a = [2:-2]
b = '1.0'+a+'1.0'
Seems the easiest way to do it for me
:D I never knew about that module! I only knew about the math module... anyways, I like my method better, more work and thinking involved xD
Never mind the fact that I love math.
Well... here's what I would do:
1) Ask the user how many rows and columns
ex: 2x2
2) Ask the user to input each number seperately
ex: 2, 3, -1, -2
3) Ask the user which letter to save the matrix to
ex: A
4) Save the matrix as:
A = ((2, 3), (-1, -2))
5) Loop that for needed matrices, then ask user which operation
ex: B = ((1, 2), (-1, -2))
6) for addition:
C_0_0 = A[0][0] + B[0][0]C_0_1 = A[0][1] + B[0][1]C_1_0 = A[1][0] + B[1][0]C_1_1 = A[1][1] + B[1][1]C_0 = C_0_0, C_0_1C_1 = C_1_0, C_1_1C = C_0, C_1C_0_0 = A[0][0] + B[0][0]
C_0_1 = A[0][1] + B[0][1]
C_1_0 = A[1][0] + B[1][0]
C_1_1 = A[1][1] + B[1][1]
C_0 = C_0_0, C_0_1
C_1 = C_1_0, C_1_1
C = C_0, C_1
Pretty much the same for subtracting. Multiplying two matrices together would be a bit tougher, but if you know how to do it by hand, you can figure it out in Python. Just find a way to loop the RxC when storing the matrix, or you can type it all out by hand
Say:
if R == 2 and C == 2: A = ((0, 0), (0, 0))if R == 1 and C == 2: A = ((0, 0))if R == 2 and C == 2:
A = ((0, 0), (0, 0))
if R == 1 and C == 2:
A = ((0, 0))
But I'm sure you can figure it out
Hey, no problem. Glad I could help
My opinion, if wanted, if I was in your situation, I would keep your laptop, not fix it, but I would buy a moniter for it. And also buy the new laptop. I'm sure you can find a cheap CRT moniter somewhere. Near where I live, there's a used computer store place, and I could get one for $50 at most. But yeah, buy the new laptop, than look around for a CRT moniter (the huge bulky ones, not LCD, in case you don't know)