为什么随机生成的数字不起作用,但一个ive手动正常工作

时间:2017-01-31 19:59:58

标签: python random

我创建的代码要求用户从数字的单词版本中输入数字的数字版本。

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")

每当这个号码是青少年时,正确的答案仍被认为是错误的。如果我将thenumber = str(random.randint(1000,9999))更改为thenumber = str(1111),则说答案1111是正确的。

我不明白为什么随机生成的青少年数字被认为是错误的,无论我输入什么答案,但如果我在代码中手动输入青少年数字,则表示这是正确的。

2 个答案:

答案 0 :(得分:1)

如果打印出// This may or may not be present package path.to.foo; public class Foo implements IFoo { public void doFooStuff() { ... } } // This is always present package path.to.my.code; public interface IFoo { public void doFooStuff(); } // Foo may or may not be present at runtime, but this always compiles package path.to.my.code; public class Test { public static void main(String[] args) { if (Arrays.asList(args).contains("--withFoo")) { Class<IFoo> fc = Class.forName("path.to.foo.Foo"); IFoo foo = (IFoo)fc.newInstance(); use(foo); } } static void use(IFoo foo) { // do something with foo } } 的值,您会看到以字为单位打印到屏幕上的数字并不总是与随机函数返回的数字相同。例如,尝试将数字设置为thenumber

这个号码不是青少年,你的代码就像青少年一样对待它,因为5 + 4&lt; 20.我认为你需要3554而不是and

or

使用pythons substring语法可以更简洁地表示同样的事情:

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

然后进一步减少到Stephen Rauch在评论中建议的内容。

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

答案 1 :(得分:0)

ln -s ~/.dotfiles/.vimrc ~/.vimrc
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
vim -c 'PluginInstall' -c 'qa!'

好的,这是有效的新代码。谢谢你的答案:)