Author Topic: python returning  (Read 1625 times)

Spazzymcgee

  • Sr. Member
  • ****
  • Posts: 255
  • Reputation: 3
  • My posts are in regard to computers
    • Free Rice
  • Computers: ASUS laptop, 32-Bit, Windows 7
  • iDevices: iPod 4, iPhone 4
python returning
« on: February 02, 2012, 07:09:47 pm »
so i have this code
Code: [Select]
import sys

import random

answer = random.randint(1,9)

guess = raw_input("What is your guess?")

if guess == answer :

    print "you win"

    sys.exit

else :

    print "you lose"

whenever it shows the "you lose" screen, i want it to return to the beginning
can someone show me how to use the "return" command
i looked online but there weren't that great of resources
(X)Learn HTML
(/)Learn SQL
()Learn JavaScript
()Learn C/C++
()Get degree in Comp Sci
()Rule the effing world

Trcx528

  • Haxor
  • Hero Member
  • *****
  • Posts: 4502
  • Reputation: 166
  • Google it!
    • iNinjas
  • Badges:
  • Computers: 13" 2011 Macbook Pro, 120 GB SSD and 16 GB of Ram
  • iDevices: None
Re: python returning
« Reply #1 on: February 02, 2012, 07:20:15 pm »
Try looping
Code: [Select]
import sys
import random

answer = random.randint(1,9)
guess = raw_input("What is your guess?")

while True:
    if guess == answer :
        print "you win"
        sys.exit
    else :
    print "you lose"
    if raw_input('Replay? [Y/n]: ') != 'Y' :
        sys.exit
Try that

Spazzymcgee

  • Sr. Member
  • ****
  • Posts: 255
  • Reputation: 3
  • My posts are in regard to computers
    • Free Rice
  • Computers: ASUS laptop, 32-Bit, Windows 7
  • iDevices: iPod 4, iPhone 4
Re: python returning
« Reply #2 on: February 02, 2012, 11:27:04 pm »
What happens if they put y though?
(X)Learn HTML
(/)Learn SQL
()Learn JavaScript
()Learn C/C++
()Get degree in Comp Sci
()Rule the effing world

A3MIRAL

  • Leader
  • Hero Member
  • *****
  • Posts: 2899
  • Reputation: 105
  • A3MIRAL -- Reporting for Duty
    • A3MIRAL
  • Badges:
  • Computers: Dell XPS15 (6 GB ram, Core i7 @ 2.0 GHz, 750 GB HDD @ 7200 RPM)
  • iDevices: iPod touch 3G 32GB, iPhone 5 32GB
Re: python returning
« Reply #3 on: February 03, 2012, 09:50:37 am »
What happens if they put y though?

that's error handling that he hasn't gotten into yet.

Trcx528

  • Haxor
  • Hero Member
  • *****
  • Posts: 4502
  • Reputation: 166
  • Google it!
    • iNinjas
  • Badges:
  • Computers: 13" 2011 Macbook Pro, 120 GB SSD and 16 GB of Ram
  • iDevices: None
Re: python returning
« Reply #4 on: February 03, 2012, 09:53:44 am »
What happens if they put y though?
If they enter Y it loops and, otherwise it will exit. 

Don't like seeing ads? Click here to register!

A12danrulz

  • Leader
  • Hero Member
  • *****
  • Posts: 4017
  • Reputation: 216
  • Badges:
Re: python returning
« Reply #5 on: February 03, 2012, 01:58:37 pm »
What happens if they put y though?
if raw_input("Replay? [Y/n]: ") != "Y" or "y":

C0deH4cker

  • Hero Member
  • *****
  • Posts: 2849
  • Reputation: 129
  • I am leaving iNinjas. Contact me via email.
  • Badges:
  • iDevices: iPhone 4S 16gb Black (5.1.1), iPad 2 32gb White (5.0.1), iPod Touch 2G 8gb (4.2.1)
Re: python returning
« Reply #6 on: February 03, 2012, 06:50:23 pm »
if raw_input("Replay? [Y/n]: ") != "Y" or "y":
This is better:

if raw_input("Play again? [y/N]: ").lower()[0] != "y":
    sys.exit()

A12danrulz

  • Leader
  • Hero Member
  • *****
  • Posts: 4017
  • Reputation: 216
  • Badges:
Re: python returning
« Reply #7 on: February 03, 2012, 06:59:36 pm »
And try using exit() instead of sys.exit(). Saves importing thd sys library, though i know someone is going to correct me on this....

C0deH4cker

  • Hero Member
  • *****
  • Posts: 2849
  • Reputation: 129
  • I am leaving iNinjas. Contact me via email.
  • Badges:
  • iDevices: iPhone 4S 16gb Black (5.1.1), iPad 2 32gb White (5.0.1), iPod Touch 2G 8gb (4.2.1)
Re: python returning
« Reply #8 on: February 03, 2012, 07:01:03 pm »
exit() is meant to be used only interactively, and is therefore bad practice to you. You sure guessed that one correctly :)

C0deH4cker

  • Hero Member
  • *****
  • Posts: 2849
  • Reputation: 129
  • I am leaving iNinjas. Contact me via email.
  • Badges:
  • iDevices: iPhone 4S 16gb Black (5.1.1), iPad 2 32gb White (5.0.1), iPod Touch 2G 8gb (4.2.1)
Re: python returning
« Reply #9 on: February 03, 2012, 07:18:54 pm »
To prevent loading the sys module, do this instead:
Code: [Select]
from random import randint

num = randint(1, 9)

while 1:
    guess = input("Guess a number between 1 and 9: ")
    if guess != num:
        print("Incorrect!")
        continue
    print("Correct!")
« Last Edit: February 03, 2012, 07:19:59 pm by C0deH4cker »

Don't like seeing ads? Click here to register!

ghoulmaster

  • Sr. Member
  • ****
  • Posts: 346
  • Reputation: 12
  • Badges:
Re: python returning
« Reply #10 on: February 04, 2012, 04:57:43 pm »
To prevent loading the sys module, do this instead:
Code: [Select]
from random import randint

num = randint(1, 9)

while 1:
    guess = input("Guess a number between 1 and 9: ")
    if guess != num:
        print("Incorrect!")
        continue
    print("Correct!")

Actually since infinite loops are frowned upon this is much better:

Code: [Select]
import random

num = random.randint(1, 9)
guess = 99999999

while guess != num:
    if guess != 99999999: print("Incorrect!")
    guess = int(input("Guess a number between 1 and 9: "))

print("Correct!")

and actually a more pythonic way of doing this :
Quote
if raw_input("Play again? [y/N]: ").lower()[0] != "y":
    sys.exit()

is to do this:

Code: [Select]
if input("Play again? [y/n]: ").lower() in {"y", "yes", "yup"}:
    sys.exit()

EDIT: you all also seem to be switching from 2.x and 3.x syntax so to clear up any further confusion can the OP say what version of python he/she is using?
« Last Edit: February 04, 2012, 04:59:21 pm by ghoulmaster »

C0deH4cker

  • Hero Member
  • *****
  • Posts: 2849
  • Reputation: 129
  • I am leaving iNinjas. Contact me via email.
  • Badges:
  • iDevices: iPhone 4S 16gb Black (5.1.1), iPad 2 32gb White (5.0.1), iPod Touch 2G 8gb (4.2.1)
Re: python returning
« Reply #11 on: February 04, 2012, 08:20:45 pm »
In both versions of python, print can be used like a function, so i prefer using it that way.

ghoulmaster

  • Sr. Member
  • ****
  • Posts: 346
  • Reputation: 12
  • Badges:
Re: python returning
« Reply #12 on: February 05, 2012, 12:55:11 pm »
In both versions of python, print can be used like a function, so i prefer using it that way.
i was talking about the switching between input and raw_input

in 2.x you should NEVER use input() but always use raw_input() and int() that if you want a number.

in 3.x input() acts like raw_input() does in 2.x so then you always want to use input()

h4ck3rpr0n3

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3063
  • Reputation: 130
  • Developer, Genius :P :P
  • Badges:
  • Computers: HP Pavillion g7: Windows 7, BT5 R1, Ubuntu 12.04, Windows 8, Linux Mint
  • iDevices: ipod touch 3g, ipod touch 2g
Re: python returning
« Reply #13 on: February 07, 2012, 08:08:00 pm »
^Agreed. I always use raw_input because frankly I hate anything higher than 2.5.1 haha. Partly because that's the highest i've learned but oh well!
goals:
[] get developer status
[X] get 30+ karma
[X] get to hero member
[X] become part of the staff

languages i know:
JavaScript
CSS
HTML
PHP
C
C++
Cython
Python

C0deH4cker

  • Hero Member
  • *****
  • Posts: 2849
  • Reputation: 129
  • I am leaving iNinjas. Contact me via email.
  • Badges:
  • iDevices: iPhone 4S 16gb Black (5.1.1), iPad 2 32gb White (5.0.1), iPod Touch 2G 8gb (4.2.1)
Re: python returning
« Reply #14 on: February 18, 2012, 11:23:20 pm »
Why not use input()?