簡體   English   中英

`JES-Python,偶數和奇數

[英]`JES - Python, Even and Odd

我需要編寫一個程序,詢問隨機數是偶數還是奇數,用戶輸入答案,然后程序會告訴您它是正確還是錯誤。 我不知道要輸入ans=raw_input("is" [random number] "Odd or Even?")

這就是我現在已經實施的方法,該怎么解決?

def evenOdd():
  num=random.randrange(1,101)
  ans=raw_input("is"+num+"even or odd?")
  if ans % 2 == 0:
    print "correct"
  elif ans % 2 == 1:
    print "incorrect"

看看python的random模塊。 您可能想要諸如random.randint(1,100)類的東西。

這應該可以解決您的問題-

def evenOdd():
    num = random.randrange(1,101)
    ans = raw_input("Is "+str(num)+" even or odd?")
    if (num % 2 == 0 and ans == "even") or (num % 2 != 0 and ans == "odd"):
        print "Correct answer"
    else:
        print "Incorrect answer"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM