Python Forum
Looping if condition isnt met
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping if condition isnt met
#1
elif time_limit_ask[0] == "r" or time_limit_ask[0] == "R":
        def random_questions():
            test_1 = input ("enter number ")
            test_2 = input ("enter number ")
            int(test_1)
            int(test_2)
            if test_1 > test_2:
                random_questions()
            else:
                pass
            intro_1_complete = True
            time_limit_exists = True
        random_questions()
I have this code above here.

Output:
enter number 4 enter number 2 enter number 4 enter number 2 enter number 5 enter number 6 Do you want a time limit per question? - or you can have random!
when number 1 is larger everything works fine, however when number 1 is smallest it should go to the next bit of code but instead it asks the same question again

whats the issue?
snippsat likes this post
Reply
#2
Your code starts with "elif".
It would seem some portion is missing.

Paul
Reply
#3
This code cannot succeed for several reasons. First int(test_1) does not transform test_1 into an integer. For this you would need test_1 = int(test_1). It means that the code is currently comparing strings. Second, the global variables intro_1_complete and time_limit_exists will not be set by the function random_questions() because in this function, the variables with that names are local variables.

Here is a more viable code
elif time_limit_ask[0] in ('r', 'R'):
    def random_questions():
        while True:
            test_1 = int(input("Enter first number ")
            test_2 = int(input("Enter second number ")
            if test_1 <= test_2:
                return
    random_questions()
    intro_1_complete = True
    time_limit_exists = True
Reply
#4
Why do you have a function definition inside other code? Usually you don't want to do that without a good reason because it's much harder to understand the flow of the program.

You've only put a snippet of your code above, and I don't think it's possible to tell what's happening. If the problem is in that function, write a small test program that calls it with number 1 smallest and show the output and explain why you think it should be doing something else.

Looks like that part of the code is working properly, (it exits when the first input is smaller), but then some other part of your code that you haven't shown starts it again.
Reply
#5
As Griboullis mentioned you are not comparing ints, you are comparing strings. When comparing strings '0' > '9' and '123456' < '2'. So your code works as long as you limit yourself to single digit numbers in the range 1 through 9. If you want to compare numbers you need to convert to a number. I would surround the conversion with try/except to prevent exiting the program if the user types something that isn't a number

It is odd defining a function inside of an if statement. It is valid Python, but really odd. It is also odd using recursion for something like this when a loop is simpler. I would write the code like this:
elif time_limit_ask[0] in ('r', 'R'):
    while True:
        try:
            if int(input("enter number ")) > int(input("enter number ")):
                break
        except ValueError:
            pass
    intro_1_complete = True
    time_limit_exists = True
Reply
#6
thanks for the help!
Reply
#7
the function calls itself. that's recursion. when it unwinds it is going to complete the finishing steps for two pairs of questions in reverse order. so you can see the results of a previous input after the results of a later input. avoid recursion unless you have a specific need for it.

code that begins with "elif" is always suspect. there may be code we cannot see. that hidden code may have bugs in it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  else condition not called when if condition is false Sandz1286 10 5,841 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,894 Jun-01-2020, 06:00 PM
Last Post: penahuse
  for some reason this isnt working lokchi2017 3 2,691 Aug-06-2018, 12:24 PM
Last Post: perfringo
  Why isnt this word length selector working Ivan 6 4,592 Mar-26-2017, 08:38 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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