Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How i can...?
#1
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
Reply
#2
(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.
Reply
#3
(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.
Reply
#4
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
   
Reply
#5
(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     :))
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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
Reply
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020