mardi 31 janvier 2017

Why does a random generated number not work but one ive put in manually work correctly

I have created code which asks the user to type in the number version of a number from a word version from a number.

import random
unit = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
teen = ["", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
tenth = ["", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
hundreth = ["", "One Hundred", "Two Hundred", "Three Hundred", "Four Hundred", "Five Hundred", "Six Hundred", "Seven Hundred", "Eight Hundred", "Nine Hundred"]
thousandth = ["", "One Thousand", "Two Thousand", "Three Thousand", "Four Thousand", "Five Thousand", "Six Thousand", "Seven Thousand", "Eight Thousand", "Nine Thousand"]
a = 11
while a>0:
  a = a - 1
  thenumber = str(random.randint(1000,9999))
  if int(thenumber[3]) + int(thenumber[2]) > 10 or int(thenumber[3]) + int(thenumber[2]) < 20:
    teencheck = "correct"
  elif int(thenumber[3]) + int(thenumber[2]) < 11 or int(thenumber[3]) + int(thenumber[2]) > 19:
    teencheck = "wrong"
  else:
    print("not possible")
  if teencheck == "wrong":
    print(thousandth[int(thenumber[0])], hundreth[int(thenumber[1])], "and", tenth[int(thenumber[2])], unit[int(thenumber[3])])
    answer = input(print("What is this number?: "))
  else:
    print(thousandth[int(thenumber[0])], hundreth[int(thenumber[1])], "and", teen[int(thenumber[3])])
    answer = input(print("What is this number?: "))
  if answer == thenumber:
      print("Correct!")
  else:
    print("That is incorrect, unlucky")

whenever the number is a teen, the correct answer is still said to be wrong. If I change the thenumber = str(random.randint(1000,9999)) to thenumber = str(1111) the answer 1111 is said to be right.

I don't understand why the randomly generated teen numbers are considered wrong no matter what answer I put in but if I manually enter a teen number in the code it says it's correct.




Aucun commentaire:

Enregistrer un commentaire