473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Is there a command for getting out of the function?

I just started with python. I tried to write a small code for guessing a number. I wondered if we can get out of the function only, not the whole program.

Expand|Select|Wrap|Line Numbers
  1. def user_guess(num):
  2.     if(num==rand_num):
  3.         print "hurrey! you guessed it right.\n"
  4.         another_try=raw_input("would you like to guess another number??\n")
  5.         if(another_try == "y" or another_try == "yes"):
  6.             num = int(input("Guess the number!\n"))
  7.             return num
  8.         else: 
  9.             print "okay! have a good day!"
  10.             sys.exit()
  11.  
  12.     elif(num>rand_num):
  13.         print "your guessed number is greater than the actual number,,\n"
  14.         guess_again=raw_input("would you like to guess the same number again??\n")
  15.         if(guess_again == "y" or guess_again == "yes"):
  16.             num=int(input("guess the number"))
  17.             user_guess(num)
  18.         else: #here i want to get out of the function only,I want the other part of the program to run
  19.     elif(num<rand_num):
  20.         print "your guessed number is lesser than the actual number,,\n"
  21.     else:
  22.  
can somebody help me out?(ignore if any other errors)
Jul 18 '19 #1
4 1114
dev7060
636 Expert 512MB
One way of doing this is to use labels and goto. But Python doesn't support them. However, you can use something like this if that serves your purpose. It enables to transfer the flow of control to a label.
Jul 18 '19 #2
Gbolly
1
I think you can use break
Jul 18 '19 #3
dev7060
636 Expert 512MB
- break statement is used to terminate the loop and jump the control to the first statement after the loop body. But here, it appears the post is meant to change the flow of control in case of functions.

- @swaroop11 The statement "sys.exit()" is responsible for the closing of the program. It can also be removed if the purpose is not so in the else case.
Jul 18 '19 #4
Rabbit
12,516 Expert Mod 8TB
You already know the command. In line 7 you get out of the function. You just need to do something similar again.
Jul 18 '19 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Clément Collin | last post by:
I working on a GIS project, with Access link which just need a little routine in VBA, but I haven't knowledges in VBA language. It's very simple, and it looks like that in a TPascal way : .......
14
by: António Marques | last post by:
Hi! I don't think I've ever been here, so I assume I'm addressing a bunch of nice people. Can you help me? I don't think there's a solution, but who knows. The thing is, picture a large...
2
Auz
by: Auz | last post by:
Hello. I have problem with getting the name of a function with javascript. The code I have works fine in firefox, but not in internet explorer. Its part of a much larger whole, but what it boils...
2
by: FunkHouse9 | last post by:
I have 2 functions, the first one, "GetSelectedItem" is determining which radio button was selected and running the "formvalidation" function if needed. This is working fine. My problem is getting...
0
by: czerwww | last post by:
Can someone please help me? I have class for database connection and I need set command.commandTimeout. How can I do that? Code: Imports System.Data.SqlClient Imports System.Data Public Class...
0
by: execrable | last post by:
I am writing a simple (maybe a bit conveluded, but I just started learning Tkinter) program that uses a 'notebook' code snippet from a python cookbook. It creates a customizable frame and radio...
3
by: dbest5250 | last post by:
hi guys i'm a little confuse about getting a variable in function and storing it in session example : <html> <head> function loadContent(paging) { <? $_SESSION=?????? (this is what...
0
by: Terry Reedy | last post by:
Rui Jorge Rei wrote: Futile, I believe. I would also like to get all the arguments and keyword The arguments to a generator function are the arguments to the generator 's next method. The...
3
vekipeki
by: vekipeki | last post by:
Hi, first of all sorry if this isn't exactly C++ related, but didn't know where it would be more appropriate. I am trying to get actual function names from their ordinal numbers from a COM dll. I...
0
by: AJ Evans | last post by:
hello all, A newbie here wondering if there is anyone out there who has some knowledge about assembly language, or translations of it. I am trying to explore a mid-sized DLL from the system32...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.