how to take (6+5) as a single input from user in python and display the result [closed] - python

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 4 years ago.
Improve this question
We are creating a calculator using python. I would like the user to input a number then operator then second number then the program prints out the result. For example, "User input calculation" ,User:5+9, and the computer prints out 14. I see its called the read-eval-print loop method. I need help converting that method into code and implemented.

Here is a simple calculator example to help get you started.
while True:
num1 = input('First Number: ')
num2 = input('Second Number: ')
op = input('Operator: ')
try:
num1 = int(num1)
num2 = int(num2)
except:
print('Input a valid number!')
continue
if op not in ['+', '-', '*']:
print('Invalid operator!')
continue
if op == '+':
print(str(num1 + num2))
elif op == '-':
print(str(num1 - num2))
elif op == '*':
print(str(num1 * num2))

Related

how to use input in a function in python [closed]

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 months ago.
The community is reviewing whether to reopen this question as of 5 months ago.
Improve this question
I just want to use input, in a function in python.
this is my code:
print("I can tell you the maximum of 3 numbers")
def max_num(num1, num2, num3, false=None):
num1 = input("enter first number")
num2 = input("enter second number")
num3 = input("enter third number")
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
elif num1.isdigit(False) and num2.isdigit(False) and num3.isdigit(False):
print("no number available")
else:
return num3
return max_num()
but when I run this code, Just first line (print), runs succesfully.
what is wrong?
I would be thankful.
When defining a function with parameters, make sure that these parameters don't come inside the definition of the function.
The code also has some indentation and logical mistakes.
This is a corrected version.
print("I can tell you the maximum of 3 numbers")
num1 = input("Enter the first number:")
num2 = input("Enter the second number:")
num3 = input("Enter the third number:")
def max_num(num1, num2, num3):
if not num1.isdigit() or not num2.isdigit() or not num3.isdigit():
return "Wrong Input"
else:
num1,num2,num3=int(num1),int(num2),int(num3)
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3
print(max_num(num1,num2,num3))
You need to call that function like print(max_num(num1, num2, num3)), you are still create the function but not calling it.
What does return max_num() even mean? You should return a number, and a number is already returned in all possible branches.
If you want to call this function, try something like print(max_num(1, 2, 3)) at the end of the script.
Also, it is a bit confusing: are you planning to pass in the three numbers by functions arguments or user input? Because you are attempting to do both right now.
Ok, there are lots of issues with this code, but to answer your original question "Why does input never get called?", the answer is simple:
You have defined the function max_num, but you have never called it. Only once a function is called does the code inside run.
So in your script, you simply need to remove all the input parameters in your function definition (as they are never used), and add the line:
max_num()
Then you'll need to fix all the other quirks :)
Happy coding

Looping a whole python function until true [closed]

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 3 years ago.
Improve this question
I'm new to python and I want to archive a function as below where a input of a user gets checked with the arithmetic operators against the case of the random typed number being true to the statement number < 100 and when not being looped by the elif function.
So I want to check the first if statement but if that is not true it should go to the elif statement and then be checked by the if statement again until it fits the criteria.
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number => 100:
number/10
userInput = int(input("Your number please: \n"))
print(unit(userInput))
SOLVED!
I could solve the problem by doing the following changes:
userInput = int(input("Your number please: \n"))
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number >= 100:
b = (number/10)
return unit(b)
print(unit(userInput))
use recursion
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number => 100:
return unit(number/10)
userInput = int(input("Your number please: \n"))
print(unit(userInput))

How to limit the number of digits input? [closed]

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 2 years ago.
Improve this question
I am requesting someone to tell me a program that asks a user to input only 9 digits. Number 0 not at start but having 9 digits.
How can I limit the number of digits?
For example:
Num=int(input('please Enter a Number'))
if num?!
Please help me soon.
If i got this right this should work:
a=input()
if len(a)==9 and a[0] != '0' and a.isdigit():
#if you want it to be integer
a=int(a)
do something
Hope it helps.
Well if I understood correctly you need a program that asks the user to enter exactly 9 digits. In that case it's pretty simple.
i = input("Enter a nine digit number: ")
if len(i) != 9:
print("You must enter 9 digits!")
quit()
if not i.isdigit():
print("Your input must be a number!")
quit()
# num is a 9 digit integer
# i is a 9 digit string
num = int(i)
You can do this.
num = input('Please enter a number')
if not num[0] == "0" and len(num) == 9 and num.isdigit():
# You can print a message or cast the num into an integer here
# like this: input_number = int(num)
print(True)
else:
print(False)
So what's happening here is that the program will accept the user input and evaluate if the first digit is NOT 0 AND that the length is exactly 9 characters.

How to debug beginners code [closed]

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 have 2 questions about my code. Why the program doesn't go in the second if statement. How can I end the loop?
from random import *
SecretNumber=randint(1,5)
Guess=int(input("Please enter Guess: "))
NumberofGuesses=1
SecretNumber=0
while Guess != SecretNumber:
NumberofGuesses=NumberofGuesses+1
if Guess>SecretNumber:
print("Please insert a smaller number")
else:
print("Please insert a bigger number")
if Guess==SecretNumber:
print("Number of Guesses: {0}".format(NumberofGuesses))
Your second if is outside the while loop, so it won't get hit until you guesss the secret number. The loop never ends because you never read another guess.
You also have a problem that you are overriding your random secret number with zero.
You need something like:
import random
SecretNumber=random.randint(1,5)
NumberofGuesses=0
while true:
Guess=int(input("Please enter Guess: "))
NumberofGuesses += 1
if Guess == SecretNumber:
break # Got it!
elif Guess>SecretNumber:
print("Please insert a smaller number")
else:
print("Please insert a bigger number")
print("Number of Guesses: {0}".format(NumberofGuesses))
It's because you're setting SecretNumber to 0. Remove it and it should work.

Keep getting a syntax error on creating a loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
num = input(float("Enter a number (enter 'q' to quit): ")
p=0
while num>=1:
p=p+1
Why do I keep getting a syntax error on the first p?
Does this mean I can't assign a numerical value on a variable?
You should close the bracket
num = input(float("Enter a number (enter 'q' to quit): "))
import sys
num = input("Enter a number (enter 'q' to quit)")
try:
num = int(num)
except:
if num == 'q':
sys.exit()
p=0
while (num>=1):
p=p+1
num = num - 1 #otherwise it will go to infinite loop

Categories