Python Forum
Adding extra question for assement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding extra question for assement
#1
Currently this program will ask you which of two numbers is bigger. To get a higher grade i need to change it so that it is randomly asking which one is bigger or smaller. How would i edit my existing code to do this?


while True:
    import sys
    import random
    user_name = input("Whats your name?")
    choice = input("{}, do you want to do custom or leveled difficulty?".format(user_name))
    if choice == "custom":
        custom_range_1 = int(input("Pick a number to be the lowest."))
        custom_range_2 = int(input("Pick a number to be the highest."))
        number_range = [custom_range_1,custom_range_2]
        first_custom_random = (int(random.randrange(custom_range_1, custom_range_2)))
        second_custom_random = (int(random.randrange(custom_range_1, custom_range_2)))
        print(first_custom_random, second_custom_random)
        user_anwser = input("Which is bigger, the first number or the second?")
        if user_anwser == ("2") and second_custom_random > first_custom_random:
            print("Correct!")
        elif user_anwser == ("1") and second_custom_random < first_custom_random:
            print("Correct!")
        else:
            print("Sorry thats wrong!")
        restart_choice = input("Do you want to restart?")
        if restart_choice == ("no"):
            print("bye!")
            sys.exit([arg])
        if restart_choice == ("yes"):
            continue
    elif choice == "leveled":
        level_choice = input("Which level would you like? Pick 1, 2, or 3")
        if level_choice == ("1"):
            points = 0
            while points < 3:
                leveled_range_1 = [1,100]
                level_1_random_1 = (int(random.randrange(1, 100)))
                level_1_random_2 = (int(random.randrange(1, 100)))
                print(level_1_random_1, level_1_random_2)
                user_anwser = input("Which is bigger, the first number or the second?")
                if user_anwser == ("2") and level_1_random_2 > level_1_random_1:
                    print("Correct!")
                    points += 1
                    print("You have {} points".format(points))                    
                elif user_anwser == ("1") and level_1_random_2 < level_1_random_1:
                    print("Correct! Thats 1 more point!")
                    points += 1
                    print("You have {} points".format(points))                    
                else:
                    print("Sorry thats wrong!")
                    points -= 1
                    print("You have {} points".format(points))                  
                    continue
            print("LEVEL 2")
            level_choice = ("2")
        if level_choice == ("2"):
            points = 0
            print("Begin level 2!")
            while points < 3:
                level_2_random_1 = (int(random.randrange(1, 1000)))
                level_2_random_2 = (int(random.randrange(1, 1000)))
                print(level_2_random_1, level_2_random_2)
                user_anwser = input("Which is bigger, the first number or the second?")
                if user_anwser == ("2") and level_2_random_2 > level_2_random_1:
                    print("Correct!")
                    points += 1
                    print("You have {} points!".format(points))                    
                elif user_anwser == ("1") and level_2_random_2 < level_2_random_1:
                    print("Correct! Thats 1 more point!")
                    points += 1
                    print("You have {} points!".format(points))
                else:
                    print("Sorry thats wrong!")
                    points -= 1
                    print("You have {} points!".format(points))
                    continue
            print("LEVEL 3!")
            level_choice = ("3")
        if level_choice == ("3"):
            points = 0
            print("Begin level 2!")
            while points < 3:
                level_3_random_1 = (int(random.randrange(1, 10000)))
                level_3_random_2 = (int(random.randrange(1, 10000)))
                print(level_3_random_1, level_3_random_2)
                user_anwser = input("Which is bigger, the first number or the second?")
                if user_anwser == ("2") and level_3_random_2 > level_3_random_1:
                    print("Correct!")
                    points += 1
                    print("You have {} points!".format(points))                    
                elif user_anwser == ("1") and level_3_random_2 < level_3_random_1:
                    print("Correct! Thats 1 more point!")
                    points += 1
                    print("You have {} points!".format(points))
                else:
                    print("Sorry thats wrong!")
                    points -= 1
                    print("You have {} points!".format(points))
                    continue
        print("You won the game!!!!!!!!!!!!!!!!!!~!!!!!!!!!")
        restart_choice = input("Do you want to restart?")
        if restart_choice == ("no"):
            print("bye!")
            sys.exit([arg])
        if restart_choice == ("yes"):
            continue
Moderator nilamo: Please use code tags in the future
Reply
#2
Right now, you use input() to ask for a number. Instead, try using random.randint(1, 100).

*edit*
Wait... there's a lot going on here, I'll need a minute to look at your code.

*edit 2*
I'm not sure what they're talking about, you're already using random numbers...
Reply
#3
(Mar-29-2017, 09:10 PM)nilamo Wrote: *edit 2*
I'm not sure what they're talking about, you're already using random numbers...

The OP wants to randomly ask the questions "Which is bigger" and "Which is smaller".

Since this is the "Homework" section, try this:

* create two functions that take the two numbers
* one function asks the "bigger" question and one the "smaller" question
* functions return true/false depending on correctness

You code draws an integer at random and uses it to determine which function to call. Incidentally you'll see that using these functions somewhat reduces the size of your code by eliminating a lot of duplication.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Forum Jump:

User Panel Messages

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