Python Input loop not taking all inputs - python-3.x

python_code
I want to take all inputs and convert them into list of lists. But this program is not taking all inputs as intended. I may have made a basic mistake but I couldn't get my head around it

Try using readline() rather than input() at every step from start. Because I don't see any problem in the loop, my best guess is because you are using input() you are not reading escape characters which are there in these kind of inputs.

Related

build a calculator for python 3

hey everyone I need your help for my programming course, I am pursuing my undergraduate degree in psychology.
The question is:
The python application you will develop will receive the mathematical operation that the user wants to calculate and print the result on the screen.
This process will continue until the user enter "Done".
The application will terminate when the user enters the end.
Conditions and restructions:
1)you will operate with positive integers
2)only 4 operations will be used
3)remember the priority of the operation
4)you will use "*" as a cross
5)no brackets will be used
6)it is forbiddento use an external module
7)you are not responsible for the user's incorrect entries
8)bulit-in functions you can only use the following:
int
float
range
print
input
len
str
max
min
you can use all functions of list and str data structure.
there are some things to be considered:
since you write, no modules are allowed, I assume, this should only run in the CLI. This will make the exercise incredibly easier, since GUI and Python are a Love-Hate-Realionship of its own.
Assuming I understood your assign right, the user will type a list of expressions like "1*2+3/4-5" for example, typing "Done" will indicate, that these should processed and the result should be printed, right? If the User types end, the script will stop working.
Also, you will only have to handle positives, this also, and the fact that you don't have to validate the user entries, makes it very easy. I won't give you a full solution, since you should learn something with this assignment, but here are some hints, that may nudge you in the right direction.
Hints:
The input will be in a string, so handling strings will be a main task. You should look up the documentation to know, in what ways you can manipulate strings. Also have a closer look at helper functions like split, if you have the word Done more than one time in the string.
Strings are in fact just lists of letters, punctuation and numbers.
Since strings are just a list, you won't have a neatly stored 22 in there, it will be a [2][2] surrounded by an operation or the words Done or end. You will have to go through your string, break it up in smaller parts, and then from there on, call functions to do whatever there is to do.
Keep in mind, that there are literally millions of ways to achieve what you have to do, if you aren't familiar with programming, keep it simple, break it up in smaller steps and then just proceed through the exercise.
Hope this will help you. If it helped you or gave you a hint in the right direction, I would appreciate an upvote.
Have fun coding.

take indefinite inputs in python separated by newlines

I want to take inputs separated by new lines, but i don't wan't it to take fixed amount of inputs. Then perform some action on given inputs. For more info look at this codechef problem.
What you can do is to use while loop and check the value of input in each iteration
input_list=[]
value=input()
while(value!=condition):
value=input()
input_list.append(value)

Stuck on a Python 3 Parable iteration

Well, we got parables exam preparations and instead of me typing everything a million times, I thought of rather making a little Python script. It's done, but something's bugged. I've been stuck on it for around 30 minutes and just can't figure it out as my Python is a bit rusty.
You can find my code at: https://repl.it/#Rrrei/CurvySecondaryService
As mentioned before, for x in x: is indeed bad naming :p. You can easily get confused about what x you are talking about, the inner or the outer one.
Also a = input(..) makes a a string, a string multiplied by a number repeats the string in Python. e.g.:
'1'*5 == '11111'
To solve this, wrap input in a int: a=int(input(...))
You are going to append the string values. Python is not typesafe and a is a string after calling input()...
Here you can see a safe example with corrected values and better names (cleancode).
check my code
It´s the correct version of yours.
If you want the same form b=0 c=0....

What Does Enumerate Do in This Context [Python]

I know that there is another answer to this but that's for complex users. I'm a basic python user who started a couple of days ago. So I need a simple answer
Im trying to understand this line of code. Mostly the enumerate part. Could someone please what enumerate does.
f = open("solutions.txt", "r")
searchlines = f.readlines()
for i, line in enumerate(searchlines):
Thanks in Advance
enumerate is used to generate a line index, the i variable, together with the line string, which is the i-th line in the text file. Getting an index from an iterable is such a common idiom on any iterable that enumerate provides an elegant way to do this. You could, of course, just initialize an integer counter i and increment it after each line is read, but enumerate does that for you. The main advantage is code readability: the i variable initialization and increment statements would be book-keeping code that is not strictly necessary to show the intent of what that loop is trying to do. Python excels at revealing the business-logic of code being concise and to the point.
You can look at Raymond Hettinger's presentation to learn more about idiomatic python from these excellent notes.

eval in template strings

I'm considering porting a rather unwieldy bash script to python but I'm stuck on how to handle the following aspect: The point of the script is to generate a png image depending on dynamically fetched data. The bash script grabs the data, and builds a very long invocation of the convert utility, with lots of options. It seemed like python's template strings would be a good solution (I would vastly prefer to stay within the standard library, since I'll be deploying to shared hosting), but I discovered that you can't evaluate expressions as you can in bash:
>>> from string import Template
>>> s = Template('The width times one is ${width}')
>>> s.substitute(width=45)
'The width times one is 45'
>>> t = Template('The width times two is ${width*2}')
>>> t.substitute(width=45)
# Raises ValueError
Since my bash script depends quite heavily on such arithmetic (otherwise the number of variables to keep track of would increase exponentially) I'd like to know if there's a way to emulate this behavior in python. I saw that this question, asking roughly the same, has a comment, reading:
This would be very unPythonic, because it's counterintuitive -- strings are just
strings, they shouldn't run code!
If this is the case, what would be a more idiomatic way to approach this problem?
The proposed answer to the question linked above is to use string formatting with either the % syntax or the format() function, but I don't think that would work well with the number of variables in my string (around 50).
Why not use built-in string formatting?
width = 45
"Width times one is {width}".format(width=width)
"Width times two is {width}".format(width=2*width)
results in
Width times one is 45
Width times two is 90
The Pythonic solution to this problem is to forget about string formatting and pass a list of arguments to one of the subprocess functions, e.g.
# I have no idea about convert's command line usage,
# so here's an example using echo.
subprocess.call(["echo", str(1 + 1), "bla"])
That way, there's no need to build a single string and no need to worry about quoting.
You probably need a better templating engine. Jinja2 supports this kind of stuff and a lot more. I don't think the standard library has anything equally powerful, but from what I figured, the library is pure Python, so you can integrate it into your application by just copying it along.
If Jinja doesn't fit you for some reason, have a look at the Python wiki, which has a section specifically for those kinds of libraries. Amongst them is the very lightweight Templite, which is only one class and seems to do exactly what you need.
The task is not that hard, why don't you just make some coding for fun? And here is the function almost does what you want.
import re
def TempEval(template,**kwargs):
mark = re.compile('\${(.*?)}')
for key in kwargs:
exec('%s=%s'%(key,kwargs[key]))
for item in mark.findall(template):
template=template.replace('${%s}'%item,str(eval(item)))
return template
print TempEval('The width times one is ${width}',width=5)
#The width times one is 5
print TempEval('The width times two is ${width*2}',width=5)
#The width times two is 10

Resources