Python programmers?

General software, Operating Systems, and Programming discussion.
Everything from software questions, OSes, simple HTML to scripting languages, Perl, PHP, Python, MySQL, VB, C++ etc.
Post Reply
User avatar
colour
Senior Member
Posts: 1597
Joined: Fri Aug 04, 2000 12:00 am
Location: Honolulu, Hawai'i

Python programmers?

Post by colour »

i need some help. im trying to create a number guessing game, eventually itll be randomly generated but for now its hard coded. im trying to have it loop, after a user plays it once, itll prompt Y/N to play again.

here is what i have so far:

Code: Select all

true=1
false=0
answer=8
repeating=true
running=true
while(repeating):
    while(running):
        inputString=raw_input("Enter a number: ")
        guess=int(inputString)
        if (guess>answer):
            print "too high"
        elif (guess<answer):
            print "too low"
        else:
            print "correct"
            running=false
    
        inputString2=raw_input("play again, (y/n)?" )
        replay=inputString2.strip()
        if (replay=='y'):
            print "ok"
        else:
            print "bye"
            repeating=false


it runs all the way through then fails to run the 2nd loop(game loop) again, it just hangs.

aslo for some odd reason i keep getting syntax errors when i try to use while(answer!=8).

help?
asdf?
User avatar
colour
Senior Member
Posts: 1597
Joined: Fri Aug 04, 2000 12:00 am
Location: Honolulu, Hawai'i

ok i figured out the indentation was wrong.

Post by colour »

works almost right now.

heres updated code:

Code: Select all

true=1
false=0
answer=8
repeating=true
running=true
while(repeating):
    while(running):
        inputString=raw_input("Enter a number: ")
        guess=int(inputString)
        if (guess>answer):
            print "too high"
        elif (guess<answer):
            print "too low"
        else:
            print "correct"
            running=false
    
    inputString2=raw_input("play again, (y/n)?" )
    eplay=inputString2.strip()
    if (replay=='y'):
        print "ok"
    else:
        print "bye"
        repeating=false


the problem now is that it loops the second block and not the game. after the "play again, (y/n)?" prompt itll loop "play again, (y/n)?" until you enter "n".

come on guys.
asdf?
User avatar
vc_wannabe
Regular Member
Posts: 487
Joined: Mon Jan 21, 2002 11:39 am
Location: SD

Post by vc_wannabe »

I took a look at it, its like 3am here and Im shot. I tested it, seems to work... Here is what the modified code is. You had it very close... If you copy this, make sure to move the first line back one space. I dont know why it keeps moving it over...

Code: Select all


true=1 
false=0 
answer=8 
repeating=true 
running=true 
while(repeating): 
    while(running): 
        inputString=raw_input("Enter a number: ") 
        guess=int(inputString) 
        if (guess>answer): 
            print "too high" 
        elif (guess<answer): 
            print "too low" 
        else: 
            print "correct" 
            running=false 
     
    inputString2=raw_input("play again, (y/n)?" ) 
    replay=inputString2.strip() 
    if (replay=='y'):
        print "ok"
        repeating=true
        running=true

    else: 
        print "bye" 
        repeating=false
        running=false
Main: AMD Ryzen R7 3700X, Asus Crosshair Hero VI, Nvidia 2070 Super, 16GB 3200MHz Ram, NVMe, EVGA SuperNOVA 750 P2 PLATINUM, Lian Li PC-V2120

Macbook Pro 16", Intel i9, AMD Radeon Pro 5500M, 16GB Ram, 1TB NVMe

Security System "NVR": Intel i7 9700k, Z390 Phantom Gaming-ITX/ac, 16GB ram, Seasonic FOCUS+ 550 Platinum, Thermaltake Core V1 Mini ITX, Cryorig H7
kolohewahine

Heres a guessing game I came up with

Post by kolohewahine »

true=1
false=0
import random
repeating=true
running=true
while(repeating):
x = random.random()
y = int(10*x) + 18
guesses = 0
while(running):
guesses = guesses + 1
if (guesses == 4):
running = false
print "Welcome to the guessing game"
inputString=raw_input("Enter a number: ")
guess=int(inputString)
if (guess > y):
print "Too high"
elif (guess < y):
print "Too low"
else:
print "Yay, you got it correct"
running=false

inputString2=raw_input("Enter a 'Y' to guess again:\n" )
replay=inputString2.strip()
replay=inputString2.upper()
if (replay=='y'):
repeating=true
running=true
elif (replay=='Y'):
repeating = true
running =true
else:
print "Thanks for playing the guessing game"
repeating=false
running=false
User avatar
ub3r_n00b
Posts: 2086
Joined: Sun Feb 16, 2003 6:31 pm
Location: Rivendel, East of The Ford of Bruinen

Post by ub3r_n00b »

you figure it out? I can probably help you out.. i know python.


-Preet
newbie101

help wanted

Post by newbie101 »

hey,
i'm a realy newbie, so pardon the simple problem. i've just finished creating my first program that calculates area of a circle for you. i want to know how i can make the program loop back to the part where they input the radius. right now, i have to restart the entire program just to get back to the input part. here is the code:

print "Area of a Circle"
radius = input("What is the radius?")
area = 3.14*radius*radius
print "The area is", area
User avatar
ub3r_n00b
Posts: 2086
Joined: Sun Feb 16, 2003 6:31 pm
Location: Rivendel, East of The Ford of Bruinen

Post by ub3r_n00b »

Do you mean loop back if input that the user entered is wrong?

just have

something = raw_input("Enter radius");
while something < 0:
something = raw_input("enter radius");



Or whatever condition you want

-Preet
newbie101

more help pleeze

Post by newbie101 »

hey,
thanx for the help, but i've got another problem. as it stands, my program will continue to repeat itself as long as the area is > 0 (Which is always usually) anyway, i want to ask the user if he wants to go back to the main menu after the area for the cirlce is displayed if he says no, i want the program to continue asking for the radius, but if he says yes, i want the program to go back to the main menu. do you know how i can do that?
User avatar
ub3r_n00b
Posts: 2086
Joined: Sun Feb 16, 2003 6:31 pm
Location: Rivendel, East of The Ford of Bruinen

Post by ub3r_n00b »

newbie101 wrote:hey,
thanx for the help, but i've got another problem. as it stands, my program will continue to repeat itself as long as the area is > 0 (Which is always usually) anyway, i want to ask the user if he wants to go back to the main menu after the area for the cirlce is displayed if he says no, i want the program to continue asking for the radius, but if he says yes, i want the program to go back to the main menu. do you know how i can do that?

insert some type of condition in the while loop

like

while something >= 0:
something = raw_input("Enter raidus, type 0 to go to menu");
if(something == 0):
break;



That will break out of the loop.

-Preet
User avatar
colour
Senior Member
Posts: 1597
Joined: Fri Aug 04, 2000 12:00 am
Location: Honolulu, Hawai'i

Post by colour »

hey thanks for the replys, i figured it out, and never came back to check this thread. sorry.

its been awhile but i think i got like a 99% on the project, got marked off because i didnt make it write to an output file correctly(syntax).
asdf?
Post Reply