2017-09-26 116 views
-1

所以我是一個Python的初學者,我不能找到一種方法,把輸入varible隨機像這樣,隨機使用Varibles? (蟒蛇)

import time 
 
import random 
 
choice = input('easy (max of 100 number), medium(max of 1000) or hard (max of 5000)? Or custom)') 
 
time.sleep(2) 
 
if choice == 'custom': 
 
    cus = input(' max number?') 
 
print ('okey dokey!') 
 
eh = 1 
 
cuss = random.randrange(0, (cus)) 
 
easy = random.randint(0, 100) 
 
medium = random.randint(0, 1000) 
 
hard = random.randint (0, 5000) 
 
if choice == 'easy' or 'medium' or 'hard' or cus: 
 
    while eh == 1: 
 
     esy = int(input('what do you think the number is?)')) 
 
     if esy > easy: 
 
      time.sleep(2) 
 
      print ('too high!') 
 
     elif esy == easy: 
 
      print (' CORRECT!') 
 
      break 
 
     elif esy < easy: 
 
      print ('TOO low') 
 
    

有沒有什麼辦法可以把一個數字,有人像第9行一樣隨機鍵入?

+0

刪除括號,並使用'(0,int(cus))' – AK47

回答

0

轉換您的變量cus爲int並把它傳遞,你通常會

cuss = random.randrange(0, int(cus)) 
1

有許多東西你的代碼錯誤。您正在使用(旗下擁有打印,這表明你使用python3這意味着,當你做cus = input(),CUS現在是一個字符串,你可能想要做cus = int(input())

choice == 'easy' or 'medium' or 'hard' or cus永遠是真實的;我不知道爲什麼人們一直在Python這樣做,但你需要做choice == 'easy' or choice == 'medium' or choice == 'hard' or choice == 'cus'。當然,更好的辦法來做到這一點是有一本字典。

values = {'easy': 100, 'medium': 1000, 'hard': 5000} 

然後做

value = values.get(choice) 
if not value: 
    value = int(input("Please enter custom value")) 

而且然後你可以 做 randomvalue = random.randint(1,value)

+0

我看到我去哪裏qrong tahnks! –