As part of a task I must write a program that copies a number with a number of unknown digits from a file. Python language.
I have created a function which retains into int several digits containing the number.
The function to read from a file:
file.read (1) This way I managed to read one character. I tried to replace the number in my variable file.read (myint) - but the reading does not work. The program ran all the way, but nothing was called.
I tried before to convert myint to int, float, long but to no avail.
Can anyone help?
Thanks .
I do not know where the discussion fits, I'd be happy to guide you.
sorry for my english..
my code part 1
my code part 2
Related
I have a problem. Last day i stuck on exercise. Id glad to use replace or smth but cant. So here it is:
Enter the sequence of characters in which they occur
structures , <#s>.
Replace each occurrence of with <#s>, and each occurrence
<#s> on <#>.
Remarks: the program should take into account that s can be as
small and large.
Be sure to use formatting when outputting data!!!
All operations in task 2 must be performed only with
variables of type bytes or bytearray!!!
i tried to write code but unfortunately i don`t have any ideas. Tried to serf internet, read some paragraphs about byte bytearry but no result.
enter image description here
so I keep getting these whitespaces in my output. I have googled but nothing relevant comes back. i have attached my code and my output. can someone tell me what i am doing wrong. I dont have any space in my code, so why is it putting it in my ouput? this is a common problem form me in zybooks and i would really love to know what i am doing wrong so i can get this done and move on to the next assignment without spending 2 hours on a simple exercise.
If you're asking why there are spaces between your outputs of favoriteColor, etc., it's because Python's print function automatically adds spaces between each of its arguments. If you don't want this happening, you can change your code in two ways:
Either add a sep argument to your print, telling Python to add an empty separator between your arguments:
print('\nFirst password:', favoriteColor, chr(95), petName, sep='')
Or, you can append your arguments together yourself, as strings:
print('\nFirst password:', favoriteColor + chr(95) + petName)
Also, I should mention that if you need an underscore, you don't need to call chr(95) -- you can just write the string '_'.
it my first time using Stack Overflow so please excuse any mistakes i have made. Im creating a program and i want ask mathematical questions for the user. But my program will generate two random numbers, and a random arithmetic operator will occur. They are plus, minus and times. So i put them into an array and this is the code.
Code:
The Error i gained:
It said the Error is in Line 10.
Ive tried doing this 'What is'+str(Ran) +,+str(op) +,+str(dom) +'?')
However i gained an invalid syntax on the comma.
Ive tried searching for this particular program, but all of them seem to have something called classes and def in. If it is possible, can i please not use the def and class in my program because i am new to python and i still need to learn what they are.
I am using Python 3.4.2, on a Windows 8 operating system if you wanted to know.
Thanks for reading, i am looking forward to have any assistance in my problem.
+ + is invalid syntax. You should be doing str(something) + ' ' + string(something_else) if you want to add two strings with a space in between. You also need quotes around add, minus and times in the list in order to make them strings.
I'm pretty new to Python so please bear with me here!
I've taken some code from ActiveState (and then butchered it around a bit) to open a DBF file and then output to CSV.
This worked perfectly well on Python 2.5 but I've now moved it to Python 3.3 and ran into a number of issues, most of which I've resolved.
The final issue I have is that in order to run the code, I've had to prefix some items with b (because I was getting TypeError: expected bytes, bytearray or buffer compatible object errors)
The code now works, and outputs correctly, except that every field is displayed as b'DATAHERE' (where DATAHERE is the actual data of course!)
So... does anyone know how I can stop it from outputting the b character? I can post code if required but it's fairly lengthy so I was hoping someone would be able to spot what I expect to be something simple that I've done wrong!
Thanks!
You are seeing the code output byte values; if you expected unicode strings instead, simply decode:
yourdata.decode('ascii')
where ascii should be replaced by the encoding your data uses.
I am trying to write a small Python 2.x API to support fetching a
job by jobNumber, where jobNumber is provided as an integer.
Sometimes the users provide ajobNumber as an integer literal
beginning with 0, e.g. 037537. (This is because they have been
coddled by R, a language that sanely considers 037537==37537.)
Python, however, considers integer literals starting with "0" to
be OCTAL, thus 037537!=37537, instead 037537==16223. This
strikes me as a blatant affront to the principle of least
surprise, and thankfully it looks like this was fixed in Python
3---see PEP 3127.
But I'm stuck with Python 2.7 at the moment. So my users do this:
>>> fetchJob(037537)
and silently get the wrong job (16223), or this:
>>> fetchJob(038537)
File "<stdin>", line 1
fetchJob(038537)
^
SyntaxError: invalid token
where Python is rejecting the octal-incompatible digit.
There doesn't seem to be anything provided via __future__ to
allow me to get the Py3K behavior---it would have to be built-in
to Python in some manner, since it requires a change to the lexer
at least.
Is anyone aware of how I could protect my users from getting the
wrong job in cases like this? At the moment the best I can think
of is to change that API so it take a string instead of an int.
At the moment the best I can think of is to change that API so it take a string instead of an int.
Yes, and I think this is a reasonable option given the situation.
Another option would be to make sure that all your job numbers contain at least one digit greater than 7 so that adding the leading zero will give an error immediately instead of an incorrect result, but that seems like a bigger hack than using strings.
A final option could be to educate your users. It will only take five minutes or so to explain not to add the leading zero and what can happen if you do. Even if they forget or accidentally add the zero due to old habits, they are more likely to spot the problem if they have heard of it before.
Perhaps you could take the input as a string, strip leading zeros, then convert back to an int?
test = "001234505"
test = int(test.lstrip("0")) # 1234505