How to write in superscript in Python 3? [duplicate] - python

This question already has answers here:
How do you print superscript in Python?
(13 answers)
Closed 1 year ago.
I am writing a code which is supposed to print a series in the form:
x - (x^2)/2! + (x^3)/3! - (x^4)/4! ... (x^n)/n!
where x and n are input from the user end.
For now my code looks like this
x = int(input('Enter number: '))
n = int(input('Till which term? '))
#for series
for i in range(1,n+1,1):
if i == n:
print('('+str(x)+'^'+str(i)+')'+'/'+str(i)+'!')
elif i == 1:
print(str(x),end = ' - ')
else:
if i%2 != 0:
print('('+str(x)+'^'+str(i)+')'+'/'+str(i)+'!',end = ' - ')
else:
print('('+str(x)+'^'+str(i)+')'+'/'+str(i)+'!',end = ' + ')
but the output is:
================= RESTART: E:/Python/Files/special series 4.py =================
Enter number: 3
Till which term? 6
3 - (3^2)/2! + (3^3)/3! - (3^4)/4! + (3^5)/5! - (3^6)/6!
is there any function to write in superscript to avoid the arrow-head sign?

As the output of Python and pretty much any language is usually in unicode or ASCII, the answer is simply no.

Related

How can I return to the top of a file after an else statement? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 months ago.
I'm trying to understand how to return to the top of a Python script from an else statement.
print('lets do some math!')
math_operation = input('Which would you like to start with, addition, subtraction or division? ')
if math_operation == "addition":
input_add1 = int(input('First number please '))
input_add2 = int(input('Second number please '))
result = input_add1 + input_add2
print(f'{input_add1} + {input_add2} = {result}')
elif math_operation == "subtraction":
input_sub1 = int(input('First number please '))
input_sub2 = int(input('Second number please '))
result = input_sub1 - input_sub2
print(f'{input_sub1} - {input_sub2} = {result}')
else:
print('I did not quite get that, lets try again')
input_div = int(input('now provide a number that is divisible from the answer'))
answer = result / input_div
print(answer)
You need to put this inside a loop,
math_operation= None
print('lets do some math!')
while math_operation != 'quit':
math_operation = input('Which would you like to start with, addition, subtraction or division? or quit')
... your code ...

Python: How do I remove the + at the end? [duplicate]

This question already has answers here:
How to avoid last comma in python loop [duplicate]
(3 answers)
Closed 1 year ago.
When I type a number, for example, 3, I want the output to be 1+2+3, not 1+2+3+, how do I fix that?
number = int(input("Please enter a number: "))
for i in range(1, number + 1):
print(i, end="+")
One easy way is to use join instead of repeated print statements.
print('+'.join(str(i) for i in range(1, number + 1)))
Or using map instead of for:
print('+'.join(map(str, range(1, number + 1))))
Try something like this:
number = int(input("Please enter a number: "))
for i in range(1, number + 1):
if i == number:
print(i)
else:
print(i, end="+")

How to not print new_lines in a while loop? [duplicate]

This question already has answers here:
Print in one line dynamically [duplicate]
(22 answers)
How to print without a newline or space
(26 answers)
Closed 2 years ago.
I have a while loop. The purpose of this while loop is to trivially decide if a given integer N exists at a given index location of M.
I can achieve a better space complexity if I only used one line for each request of an input. Arbitrarily giving one element at a time instead of an entire list.
M = 2
N = 2
index = -1
while True:
print('take input: ', end = "")
a = str(input())
index = index + 1
if int(a) == N:
if index == M:
print('yes')
break
if index > M:
print('no')
break
if index < M:
if int(a) == N:
print('no')
break
Unintended result
================= RESTART: C:\Users\User\Desktop\logspace(m).py ================
take input: 1
take input: 2
no
>>>
I would like the console to only do one line at a time as shown below.
================= RESTART: C:\Users\User\Desktop\logspace(m).py ================
take input: (Do not echo previous inputs only one integer at a time... All on one line!)
no
>>>
Question
Is there any functions, modules, etc that could do this without tinkering with my console settings?

Problem with repeating program in python3 [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
How do i get this program repeat when 'again' gets 'Y' or 'y'? Yesterday the same code worked smh, but today it closes the program whatever i write in there) And yeah...tabulation is wrong but it's because stackoverflow copied it in some weird way :))
while True:
start = input("What do you want to do? + - * / ")
if start == '+':
x = float(input("digit 1 "))
y = float(input("digit 2 "))
res = x + y
print('The result is ' + str(res))
again = input('Do u want to try again? Y/N ')
if again == 'N' or 'n':
break
Look, you are using the wrong syntax for checking the condition. Use this syntax :
if again=='N' or again=='n':
break

Comparison between string cell to list cell in python [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
def show_hidden_word(secret_word, old_letters_guessed):
i = 0
new_string = ""
while i < len(secret_word):
j = 0
print(1)
for j in old_letters_guessed:
if secret_word[i] == old_letters_guessed[j]:
new_string += secret_word[i]
print(old_letters_guessed[j])
j += 1
print(secret_word[i])
i += 1
return new_string
Why the comparate between those string don't work?And can someone help to fix it?
This should help. You can convert the input to int or 9 to string '9'
choice = input('Enter an option - out of the loop')
while int(choice) != 9: #or while choice != '9'
menu(list_of_products, choice)
choice = input('Enter an option - in the loop')
Maybe choice is a string and you need to convert it to int.

Categories

Resources