Home > Software engineering >  I want to make sure the user 's input is in the same order as the list. If it is not, it should
I want to make sure the user 's input is in the same order as the list. If it is not, it should

Time:10-27

solution = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
enter number >1
enter number >1
enter number >2
enter number >3
enter number >5
enter number >8
enter number >3
Try again

What I tried

guess = int(input("Enter the next Fibonacci number >"))

solution = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

while guess not in solution or guess < 50:
    guess = int(input("Enter the next Fibonacci number >"))
    # Use if condition to compare
    if guess not in solution[0:] and guess not in solution:
        print("Try again")
        break

CodePudding user response:

You can the following a try, where it makes you keep trying until you say the last number in the solution:

solution = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

i = 0
while i < len(solution):
    guess = int(input("Enter the next Fibonacci number >"))
    # Use if condition to compare
    if guess != solution[i]:
        print("Try again")
    else:
        i  = 1

CodePudding user response:

not sure if this is what you were asking for, but here is what i would do

solved = 'FALSE'
while solved == 'FALSE':
  solution = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
  answers = []
  while len(answers) < 10:
    answers.append(int(input('Enter the next Fibonacci number >')))

  if answers == solution:
    print('correct')
    solved = 'TRUE'
  else:
    print('try again')
    solved = 'FALSE'

CodePudding user response:

You can use this.

solution = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
guess_num = 1

while guess_num < 10:
    guess = int(input("Enter the next Fibonacci number >"))
    if guess != solution[guess_num-1]: print("Try again"); break
    guess_num  = 1
  • Related