noobsicle
noobsicle

Reputation: 1

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.

Upvotes: 0

Views: 96

Answers (2)

noobsicle
noobsicle

Reputation: 1

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))
  teen_check = (10 < int(thenumber[2:4]) < 20)
  if not teen_check:
    print(thousandth[int(thenumber[0])], hundreth[int(thenumber[1])], "and", tenth[int(thenumber[2])], unit[int(thenumber[3])])
    answer = input("What is this number?: ")
  else:
    print(thousandth[int(thenumber[0])], hundreth[int(thenumber[1])], "and", teen[int(thenumber[3])])
    answer = input("What is this number?: ")
  if answer == thenumber:
      print("Correct!")
  else:
    print("That is incorrect, unlucky")

Ok, this is the new code that works. Thanks for the answers :)

Upvotes: 0

Aaron
Aaron

Reputation: 2053

If you print out the value of thenumber you'll see that the number being printed to the screen in words is not always the same as the number returned from the random function. For instance, try setting the number to 3554.

This number is not a teen, your code treats it like a teen because 5+4 < 20. I think you need an and rather than an or.

if int(thenumber[2] + thenumber[3]) > 10 and int(thenumber[2] + thenumber[3]) < 20:
    teencheck = "correct"
  else:
    teencheck = "wrong"

The same thing could be represented this way more concisely using pythons substring syntax:

  if int(thenumber[2:4]) > 10 and int(thenumber[2:4]) < 20:
    teencheck = "correct"
  else:
    teencheck = "wrong"

Which then further reduces to what Stephen Rauch suggested in the comments.

  if 10 < int(thenumber[2:4]) < 20:
    teencheck = "correct"
  else:
    teencheck = "wrong"

Upvotes: 1

Related Questions