Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I'm struggling to implement a program that prompts the user for an arithmetic expression,
(for example; 1 + 1) and then calculates and outputs the result as a floating-point value formatted to one decimal place (for example; 2.0).
Also, I want there to be a space between x and y, and a space between y and z in the equation (for example; 1 + 1, with spaces between each character).
This is basically my desired output (in a terminal):
https://i.stack.imgur.com/5LuRH.png
I'm pretty new to python so this will probably seem easier to more experienced coders, but anyone have any ideas, and thanks in advance.
You can just simply use eval directly on the expression inserted...
expression = str(input('Expression: '))
print("{:.1f}".format(eval(expression), 1))
output:
Expression: 1 + 1
2.0
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I have a simple program that takes data from the user. Here is an abbreviated version of it:
a = "0-1"
b = "0‑1"
print(a in b) # prints False
Problem:
ord('-') for a = 45
ord('‑') for b = 8209
How can I make sure that the "-" sign is always the same and checking a in b returns True?
The most robust way would be to use the unidecode module to convert all non-ASCII characters to their closest ASCII equivalent automatically.
import unidecode
print(unidecode.unidecode(a) in unidecode.unidecode(b))
It's not clear if your example is part of a more general, but for the example provided you can handle this using replace:
a = "0-1"
b = "0‑1"
print(a.replace("‑", "-") in b.replace("‑", "-")) # True
I've called replace on both sides, because it's not clear which side is your input and which is not. In principle though this comes down to "sanitize your input".
If this is more of a general problem, you might want to look at using .translate to produce a mapping of characters to apply in one go.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I am having a problem with my addition. The way I set it up is to take a problem (ex 5+3) and assign each number to a variable. I converted them to integers using int() but when I run my script it would come back as 53. Does anyone know a solution?
I am very new to python so my code is a mess.
string = (pr)
new_string = string.replace("+", " " )
txt = (new_string)
x = txt.split(" ")
a,b = x
int(a)
int(b)
print (a + b)
Because int(a) does not change a, the generated integer number becomes an unused temporary variable. So a is still of a string type.
You need to use a=int(a) as well as next line b=int(b)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Id codes at a company come in the form x-y-zzzzzz, where x is a digit and y is a letter and zzzzzz represents a string of 6 letters. Write a function which takes in a code as an input (e.g. 3-a-abaabb) and returns the zzzzzz part (e.g. abaabb).
I have no idea how to start and solve this question. any help would be much appreciated. My IDE is pycharm (solving python coding problems) I basically need to create a function which takes the code as an input and will return the last 6 letters
You can use str.spit('-') then search count in code with repeat is equal or not, like below:
def fnd_code(code):
repeat, char, search = code.split('-')
return search.count(char) == int(repeat)
print(fnd_code('3-a-abaabb'))
print(fnd_code('4-a-abaabb'))
Output:
True
False
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to strip 0 from a given string.
The string contains either 1 or 0. I want to strip the zeroes if they appear at the ends.
I know i can do this using if condition, but i want to know if there is any function made to do this efficiently than using if-else.
Example-
String = 0100010101010
Output = 10001010101
Also, i don't think using regex is any more efficient, complexity wise.
Try this:
s = "0100010101010"
print(s.lstrip("0").rstrip("0"))
'10001010101'
This should work for the string s:
s = s.strip("0")
Make sure s is a string and not a number.
Can you try this , it will work
s = str(s).strip("0")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i = 1
while i <= 6:
print 2**i,' ','\t', 3**i
i += 1
print
i = 1
while i <= 6:
print 2**i ,'\t', 3**i
i += 1
print
This is the two different code I wrote. For the first one, I add four spaces and the output is strange. If I change the four spaces into three spaces, the output of the two codes will be the same. I don't understand it.
Output:
If you want fixed, formatted output you should look into formatted print statements instead of using tabs. This site has a good description: https://pyformat.info/ The padding and aligning section is probably what you want to start with to help clean up your output.