Python Forum

Full Version: How i can...?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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  Huh
(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.
(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.
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
   
(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!!!!   Heart     :))
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.
ok ok ok .. thankyou for helping me.

but i can't see a  ' like '  button !!! how i can like one post???    Think    Confused Confused Confused
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.
(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