Posts: 30
Threads: 9
Joined: Dec 2016
hi guys
i'm new in python
how i can goto a line ...
How i can use this code, i wish my program if inpt is not '+-*/' try to re input
mylabel
inpt=input('enter operator')
if str(inpt)=='+' or str(inpt)=='-' or str(inpt)=='*' or str(inpt)=='/' :
if inpt=='+' :
pos()
if inpt=='*'
multiply()
if inpt=='/'
div()
if inpt=='-'
neg()
else:
goto mylabel
# thanks
Posts: 2,342
Threads: 62
Joined: Sep 2016
(Dec-06-2016, 08:36 PM)sajley Wrote: how i can goto a line ... You can create a function, but there's no goto-label in Python. That's generally a deprecated practices in modern times, other than in some very critical low-level code, for which you should probably not be using Python anyway.
Here, rather than a function, you probably want a while loop.
Posts: 30
Threads: 9
Joined: Dec 2016
Dec-07-2016, 05:40 PM
(This post was last modified: Dec-07-2016, 05:44 PM by snippsat.)
(Dec-06-2016, 08:49 PM)micseydel Wrote: You can create a function, but there's no goto-label in Python. That's generally a deprecated practices in modern times, other than in some very critical low-level code, for which you should probably not be using Python anyway.
Here, rather than a function, you probably want a while loo thankyou
*** how changing this code, to be correct code for my mean
def pos(a,b):
c=a+b
return c
def neg(a,b):
c=a-b
return c
def mult(a,b):
c=a*b
return c
num1=input('Number1: ')
num2=input('Number2: ')
opr=input('Enter Operator: + or - or * :')
ok='2'
sum=0
if str(opr)=='+' or str(opr)=='-' or str(opr)=="z" :
ok='1'
if str(opr)=='+':
sum=pos(int(num1),int(num2))
if str(opr)=='-':
sum=neg(int(num1),int(num2))
if str(opr)=="z"
sum=mult(int(num1),int(num2))
else:
print('Enter Operator: + or - or *')
if ok=='1' :
print(str(sum)) ****** if user input a wrong operator, program try to re input operator :)
************** trying to reinput correct operator, only + or - or *
Edit admin:
Use code tag.
For help push BBCode help button.
Posts: 3,458
Threads: 101
Joined: Sep 2016
With a while loop.
running = True
while running:
num1=input('Number1: ')
num2=input('Number2: ')
operator = input('Operator (* + / -, leave empty to quit): ')
if not operator:
running = False
else:
# do something with the numbers
Posts: 30
Threads: 9
Joined: Dec 2016
(Dec-07-2016, 06:55 PM)nilamo Wrote: With a while loop.
Code:
thankyou
for this line:
operator = input('Operator (* + / -, leave empty to quit): ')
if operator == T (for example letter T) what happen??
only * - / +, else try to input again +-*/!
******* Sorry ! i understand a little english, and cant write good!!!! :))
Posts: 4,223
Threads: 97
Joined: Sep 2016
If the user inputs an invalid operator (like 'T'), you would print a message that the operator is invalid, and let the code return to the top of the loop. So in nilamo's code where it says "# do something with the numbers", you would first check for the valid operators and handle them, but if the operator isn't any of the valid ones, you would print the message about the invalid operator.
Posts: 30
Threads: 9
Joined: Dec 2016
Posts: 4,223
Threads: 97
Joined: Sep 2016
There's a like button under each post (except your own) on the right side. There's also a 'Rep' button on the bottom right of each post to give people reputation points.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Dec-08-2016, 09:39 PM)ichabod801 Wrote: There's a like button under each post (except your own) on the right side. There's also a 'Rep' button on the bottom right of each post to give people reputation points.
*poke*
http://python-forum.io/Thread-User-Requi...91#pid5991
|