Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] Beginner's code
#1
Hey there, started learning today and i'm currently trying to make an '8 ball' from a challenge online.
I'm trying to make it so when the player says NOT to terminate the program, and keep the game going, it will return to the starting point.
Also, would love to get some advice on how to get the code more efficient and tidy for the future :)

import random
import time

list = ["It is certain",
           "It is decidedly so",
           "Without a doubt",
           "Yes definitely",
           "You may rely on it",
           "As I see it", " yes",
           "Most likely",
           "Outlook good",
           "Yes",
           "Signs point to yes",
           "Reply hazy try again",
           "Ask again later",
           "Better not tell you now",
           "Cannot predict now",
           "Concentrate and ask again",
           "Don't count on it",
           "My reply is no",
           "My sources say no",
           "Outlook not so good",
           "Very doubtful"]
int = 1
def Interface():
    print("Calculating..")
    time.sleep(3)
    return ("\n" + random.choice(list) + "\n")

while True:
    if int == 1:
        print("Start a new game by and ask a question")
        user_input = input("> ")
        if user_input[-1] != "?":
            print("You should ask in a form of a question..")
        else:
            print(Interface())
            int += 1
    if int != 1:
        print("Finish the game? (Yes/No)")
        user_answer = input("> ")
        if (user_answer.lower() == "yes"):
            print("Goodbye")
            break;
        if (user_answer.lower() == "no"):
            int == 1 
            
Reply
#2
You can control the flow better if you write a higher level code structure, for example
def play_game():
    while True:
        display_welcome_to_the_new_game()
        while True:
            process_one_question()
            if we_want_to_quit():
                say_goodbye()
                return
play_game()
Now you have smaller independent functions to write.

About the style: don't use well known words from the python library such as 'int' or 'list' for variable names. Use your own names, for example all_answers = [...]
Reply
#3
Thanks dude! helped alot
Reply
#4
Note that if (user_answer.lower() == "yes"): is equivalent to if user_answer.lower() == "yes":. I only use parentheses in conditions if they get complicated. Same with return ("\n" + random.choice(list) + "\n").
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner: Code not work when longer list raiviscoding 2 812 May-19-2023, 11:19 AM
Last Post: deanhystad
  Code not reading http link from .txt file (Beginner level) plarrip 3 2,394 Dec-17-2020, 11:33 PM
Last Post: bowlofred
  Beginner: I need help understanding few lines of a code. hop_090 1 1,679 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,733 Jun-18-2020, 04:59 PM
Last Post: QTPi
  A beginner code... TheDude 7 3,270 Jun-18-2020, 05:39 AM
Last Post: TheDude
  [Beginner] Code is not producing desired result fakej171 2 2,421 Mar-21-2020, 10:26 AM
Last Post: buran
  what function should i use to tidy up my code (extreme beginner) scraig0117 4 2,278 Dec-16-2019, 04:03 PM
Last Post: scraig0117
  Beginner at Python. Trying to count certain integer from random string of code kiaspelleditwrong 3 2,407 Oct-14-2019, 10:40 AM
Last Post: perfringo
  Beginner trying to code in python RA0211 1 1,829 Sep-26-2019, 11:10 AM
Last Post: emryscass
  Beginner Code bh3282 9 4,495 Mar-18-2019, 03:58 PM
Last Post: samsonite

Forum Jump:

User Panel Messages

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