初学者Python-Looping到程序的开头

时间:2014-04-19 16:07:34

标签: python while-loop

我会重新开始!我已经包含了完整的代码,因此您可以获得我想要做的图片。问题(1)我无法回到最开始。问题(2)它不是循环@the代码的Higher_or_lower部分。它只是通过if和else higher_or_lower语句。感谢

done = False

while done == False:

    import random


    def is_same(targ, num):
        if targ == num:
            result="WIN"
        elif targ > num:
            result="LOW"
        else:
            result="HIGH"
            return result



    User_Name = input("What is your name?\n:")
    print("\nWelcome", User_Name, "To the guessing number game")
    print( """\n\n
                GUESSING NUMBER GAME
          ----------------------------------
          ##################################
          #                                #
          #     [E] For Easy 1 - 10        #
          #                                #
          #     [M] For Medium 1 - 50      #
          #                                #
          #     [H] For Hard 1 - 100       #
          #                                #
          ##################################
          """ )

    Choose_Level = input("\t\tPlease choose your Level : " )


    while Choose_Level != "E" and Choose_Level != "M" and Choose_Level != "H":
        Choose_Level = input("Sorry. You must type in one of the letters 'E', 'M', 'H'\n:")


    if Choose_Level == "E":
        print("You have chosen the Easy Level! You are a wimp by nature")
    elif Choose_Level == "M":
        print("You have chosen the Medium Level! Can you defeat the game?")
    else:
        print("You have chosen the Hard Level! You are a Guessing Number warrior")


    if Choose_Level == "E":
        computer_number = random.randint(1,10)
    elif Choose_Level == "M":
        computer_number = random.randint(1,50)
    else:
        computer_number = random.randint(1,100) 


    guess = int(input("Can you guess the number? \n:"))




    higher_or_lower = is_same(computer_number, guess)

    counter = 1


    while higher_or_lower != "WIN":
        counter +=1
        if higher_or_lower == "LOW":
            print("Guess attempts", counter)
            guess = int(input("\nSorry, you are to low. Please try again.\n:"))
        else:
            print("Guess attempts", counter)
            guess = int(input("\nSorry, To high. Please try again. \n:"))
            higher_or_lower = is_same(computer_number, guess)
            input("Correct!\nWell done\n\n")
            print( """
            ##############################
            #                            #
            #    [S] Play again          #
            #                            #
            #    [E] Exit                #
            #                            #
            ##############################
            """)

            start_again = (input("\t\t Please choose a option 'S' or 'E' "))


            while start_again != "S" and start_again != "E":
                start_again = (input("You must enter a upper case letter 'S' or 'E' to continue"))

            if start_again == "S":
                done = False
            else:

                print("Thanks for playing the number game. Goodbye")
                done = True
                breaK

2 个答案:

答案 0 :(得分:1)

好的,关于Python缩进如何工作的入门知识。

number = 1
while number < 10:
    print(number)
    number += 1
    print("this will print everytime because it's inside the while")

Output:

1
this will print everytime because it's inside the while
2
this will print everytime because it's inside the while
3
this will print everytime because it's inside the while
4
this will print everytime because it's inside the while
5
this will print everytime because it's inside the while
6
this will print everytime because it's inside the while
7
this will print everytime because it's inside the while
8
this will print everytime because it's inside the while
9
this will print everytime because it's inside the while

请注意,由于它的缩进,每次都会打印第二张打印件。 现在是第二个例子:

number = 1
while number < 10:
    print(number)
    number += 1
print("this will print AFTER the loop ends")

输出:

1
2
3
4
5
6
7
8
9
this will print AFTER the loop ends

看看它如何仅在循环结束后打印?那是因为缩进。你应该纠正你的代码并正确地缩进它......

好的,我更正了代码,现在似乎正在运行。大多数是因为缩进。请尝试了解它是如何工作的......

import random 

def is_same(targ, num):
    if targ == num:
        result="WIN"
    elif targ > num:
        result="LOW"
    else:
        result="HIGH"
    return result #this was out indented


User_Name = input("What is your name?\n:")
print("\nWelcome", User_Name, "To the guessing number game")
print( """\n\n
            GUESSING NUMBER GAME
      ----------------------------------
      ##################################
      #                                #
      #     [E] For Easy 1 - 10        #
      #                                #
      #     [M] For Medium 1 - 50      #
      #                                #
      #     [H] For Hard 1 - 100       #
      #                                #
      ##################################
      """ )
Choose_Level = input("\t\tPlease choose your Level : " )

while Choose_Level != "E" and Choose_Level != "M" and Choose_Level != "H":
    Choose_Level = input("Sorry. You must type in one of the letters 'E', 'M', 'H'\n:")

if Choose_Level == "E":
    print("You have chosen the Easy Level! You are a wimp by nature")
elif Choose_Level == "M":
    print("You have chosen the Medium Level! Can you defeat the game?")
else:
    print("You have chosen the Hard Level! You are a guessing number warrior")

if Choose_Level == "E":
    computer_number = random.randint(1,10)
elif Choose_Level == "M":
    computer_number = random.randint(1,50)
else:
    computer_number = random.randint(1,100)

guess = int(input("Can you guess the number? \n:"))



higher_or_lower = is_same(computer_number, guess)

counter = 1


while higher_or_lower != "WIN":
    counter +=1
    if higher_or_lower == "LOW":
        print("Guess attempts", counter)
        guess = int(input("\nSorry, you are to low. Please try again.\n:"))
    else:
        print("Guess attempts", counter)
        guess = int(input("\nSorry, To high. Please try again. \n:"))
    higher_or_lower = is_same(computer_number, guess) # this was out indented
print("Correct!\nWell done\n\n") # this all the way to the bottom was out indented
print( """
##############################
#                            #
#    [S] Play again          #
#                            #
#    [E] Exit                #
#                            #
##############################
""")

start_again = (input("\t\t Please choose a option 'S' or 'E' "))


while start_again != "S" and start_again != "E":
    start_again = (input("You must enter a upper case letter 'S' or 'E' to continue"))

if start_again == "S":
    done = False
else:
    print("Thanks for playing the awesome number game. Goodbye")
    done = True

答案 1 :(得分:0)

看起来添加一点结构会帮助你。

这样的事情可能会帮助您完成控制流程。希望为您提供一个有意义的大纲,并使循环多次运行变得更容易。

""" Guess the number game.
"""
import random


def is_same(targ, num):
    """ Check for correct entry. """
    if targ == num:
        result = "WIN"
    elif targ > num:
        result = "LOW"
    else:
        result = "HIGH"

    return result


def play_game():
    """ Play the game 1 time, returns play_again """
    User_Name = input("What is your name?\n:")
    print("\nWelcome", User_Name, "To the guessing number game")
    print("""\n\n
                GUESSING NUMBER GAME
          ----------------------------------
          ##################################
          #                                #
          #     [E] For Easy 1 - 10        #
          #                                #
          #     [M] For Medium 1 - 50      #
          #                                #
          #     [H] For Hard 1 - 100       #
          #                                #
          ##################################
          """)

    Choose_Level = input("\t\tPlease choose your Level : ")

    while Choose_Level != "E" and Choose_Level != "M" and Choose_Level != "H":
        # this could be simplified to: while choose_level not in 'EMF':
        Choose_Level = input("Sorry. You must type in one of the letters " +
                "'E', 'M', 'H'\n:")

    if Choose_Level == "E":
        print("You have chosen the Easy Level! You are a wimp by nature")
    elif Choose_Level == "M":
        print("You have chosen the Medium Level! Can you defeat the game?")
    else:
        print("You have chosen the Hard Level! " +
                "You are a Guessing Number warrior")

    if Choose_Level == "E":
        computer_number = random.randint(1, 10)
    elif Choose_Level == "M":
        computer_number = random.randint(1, 50)
    else:
        computer_number = random.randint(1, 100)

    counter = 1
    higher_or_lower = ""
    while higher_or_lower != "WIN":
        guess = int(input("Can you guess the number? \n:"))
        higher_or_lower = is_same(computer_number, guess)
        counter += 1

        if higher_or_lower == "LOW":
            print("Guess attempts", counter)
            print("\nSorry, you are to low. Please try again.\n:")
        elif higher_or_lower == "HIGH":
            print("Guess attempts", counter)
            print("\nSorry, To high. Please try again. \n:")
        else:
            print("Correct!\nWell done\n\n")
            break

    print("""
    ##############################
    #                            #
    #    [S] Play again          #
    #                            #
    #    [E] Exit                #
    #                            #
    ##############################
    """)

    start_again = input("\t\t Please choose a option 'S' or 'E' ")

    while start_again != "S" and start_again != "E":
        # can simplify: while start_again not in 'SE':
        start_again = input("You must enter a upper case letter" +
                "'S' or 'E' to continue")

    if start_again == "S":
        return True
    else:
        return False


def main():
    """ Main loop for playing the game. """
    play_again = True
    while play_again:
        play_again = play_game()

    print("Thanks for playing the number game. Goodbye")


if __name__ == '__main__':
    main()