2012-09-27 43 views
4

我是編程的新手,並試圖通過使用python來介紹類來學習它。如何比較兩個兩位數的整數是一樣的,一次翻轉和一個字符

我的一個任務的要求我們做到以下幾點:

  1. 比較用戶生成的兩位數整數隨機生成的兩位數整數。

    1. 如果兩個隨機的,並且用戶產生的整數匹配打印blah1
    2. 如果用戶生成的整數具有相同的兩個數字中的反向隨機生成的整數,打印blah2
    3. 如果用戶生成的整數具有一個數字與隨機生成的整數相同,print blah3。

現在,到目前爲止,我們只學會了基本的東西,(運營商,if/else/elifwhile環,印刷,字符串,整數)

我想出的東西,隨機分配兩位數字,將它們轉換爲字符串,然後將它們連接成兩位數字符串。從這裏,我用elif語句來匹配每個可能的條件。

不幸的是,這不是必需的。當我進行比較時,我必須使用兩位數的整數。不幸的是,我完全不知道如何比較一個整數的部分,或者將整數與我所教導的整數相反。

現在,我不想找人爲我解決這個問題。我希望得到一些幫助,或者提示或者建議我應該如何用我擁有的基本知識思考這個問題。

任何和所有的幫助,非常感謝。

我已經包含了我編寫的代碼。

# homework 2 
# problem 1 
# 
# lottery guessing program 
# 
# the program randomly generates a two-digit number, prompts the user to enter a two-digit number, 
# and determines whether the user wins according to the following rules: 
# 
# 1. if both digits match in the right order, you will win $10,000. 
# 2. if both digits match, but in the reversed order, you will win $3,000. 
# 3. if you match one digit in either place, you will win $1,000 

#imports 
import random 

#rules 
print("guess a number. if both digits match in the right order, you will win $10,000."\ 
     "\nif both digits match, but in the reversed order, you will win $3,000." \ 
     "\nif you match one digit in either place, you will win $1,000." \ 
     "\nif you don't match any digits, you do not win anything.") 

# random variables 
rand_num1 = str(random.randint(1,9)) 
rand_num2 = str(random.randint(1,9)) 

#ask user for number 
user_num1 = input("what is your first number? ") 
user_num2 = input("what is your second number? ") 

#for testing purposes, if testing, comment out the previous two lines 
#combd_num = (str(rand_num1)) + (str(rand_num2)) 
#revsd_num = (str(rand_num2)) + (str(rand_num1)) 
#print(rand_num1) 
#print(rand_num2) 
#print(combd_num) 
#print(revsd_num) 
#user_num1 = input("what is your first number? ") 
#user_num2 = input("what is your second number? ") 
#ucomb_num = (str(user_num1)) + (str(user_num2)) 

#output the numbers 
print("the number is, ", (rand_num1 + rand_num2),"\ 
     \nyour number is, ", (user_num1 + user_num2), sep="") 

#elif statement 
if (user_num1 + user_num2) == (rand_num1 + rand_num2): 
    print("you guessed the exact number. you win $10,000!") 
elif (user_num2 + user_num1) == (rand_num1 + rand_num2): 
    print("you guessed both digits but in reverse. you win $3,000!") 
elif user_num1 == rand_num1 and user_num2 != rand_num2: 
    print("you guessed one digit right. you win $1,000!") 
elif user_num1 == rand_num2 and user_num2 != rand_num2: 
    print("you guessed one digit right. you win $1,000!") 
elif user_num2 == rand_num1 and user_num1 != rand_num2: 
    print("you guessed one digit right. you win $1,000!") 
elif user_num2 == rand_num2 and user_num1 != rand_num2: 
    print("you guessed one digit right. you win $1,000!") 
else: 
    print("sorry, you didn't guess the right number.") 

回答

0

我認爲你面臨的挑戰是如何從兩位數的整數中得到獨立的數字。有可能使用數學運算符。

只要想想像42這樣的數字代表每個數字。 4是「十」數字,2是「一」數字。反過來想想它可能會有所幫助。如果你在整數42在單獨的變量,你將如何組裝成單個整數42

希望我的提示能幫助你!如果我太斜了,我可以試着再詳細一點,但我確實希望你能夠自己解決它,並且我懷疑你一旦想到了正確的操作員就會很快得到它。使用。

4

有三個技巧,將幫助你在這裏:

  1. 列表可以像字符串和數字進行比較。整數列表可以比作另一個,如果包含的數字相同,比較返回True

    >>> [1, 2] == [1, 2] 
    True 
    >>> [1, 3] == [1, 2] 
    False 
    
  2. 可以輕鬆倒車列表。您可以使用內置函數reversed(),或者您可以使用[start:stop:stride]切片符號來提供負向步幅。後者給你一個顛倒列表太:

    >>> list(reversed([1, 2])) 
    [2, 1] 
    >>> [1, 2][::-1] 
    [2, 1] 
    

    reversed()函數返回一個迭代,通過傳遞到list()構造我們再次獲取列表。

  3. 您可以使用in運算符來測試列表成員資格。使用此測試是否個別整數整數列表的一部分:

    >>> 1 in [1, 2] 
    True 
    >>> 3 in [1, 2] 
    False 
    

這些招數3一起應該給你所有你需要重新規劃你的腳本與整數工作的工具。將您的隨機數字和用戶輸入(使用int()函數轉換爲整數)存儲在列表中,並從那裏開始工作。

如果必須接受一個整數輸入10周99之間的事情變得有點棘手。您可以通過使用次模%和除法\運營商分離出10秒和1秒:再次

tens, ones = divmod(number, 10) 

現在你有兩個獨立的整數:

ones = number % 10 
tens = number // 10 

您可以使用divmod() function將二者結合起來操作與你做比較。

+0

薩米爾也可能會發現它有用知道,明顯'[1,2] ==逆轉([2,1])''給FALSE',你必須使用'[1,2] ==列表(反轉([2,1]))'或切片符號來得到一個列表進行比較。順便說一句,我不相信你使用'reversed()'給出的例子。 – Duncan

+0

在Python 3中,正確的是一個迭代器,我仍然經常使用Python 2。我會糾正的。 –

+0

不是'revers'在Python 2中返回一個'listreverseiterator object'? – DSM

0

好的,謝謝大家的幫忙。由於您的建議和提示,我設法根據規格完成作業。

雖然我並不爲自己的工作方式感到驕傲 - 對我來說這似乎很sl - - 它很有用。

那就是:

#imports 
import random 

#rules 
print("guess a number. if both digits match in the right order, you will win $10,000."\ 
     "\nif both digits match, but in the reversed order, you will win $3,000." \ 
     "\nif you match one digit in either place, you will win $1,000." \ 
     "\nif you don't match any digits, you do not win anything.") 

#random variable 
rand_num = random.randint(0,99) 
print(rand_num) 
#separate the digits 
rand_num_tens = rand_num//10 
print(rand_num_tens) 
rand_num_ones = rand_num%10 
print(rand_num_ones) 
#reverse the random number digits 
revsd_rnd_num = (rand_num_ones * 10) + rand_num_tens 
print(revsd_rnd_num) 

#ask user for number 
user_num = int(input("guess a number between 1 and 99: ")) 
#separate the digits 
user_num_tens = user_num//10 
print(user_num_tens) 
user_num_ones = user_num%10 
print(user_num_ones) 

#output the numbers 
print("the number is, ", rand_num,"\ 
     \nyour number is, ", user_num, sep="") 

#elif statement 
if rand_num == user_num: 
    print("you guessed the exact number. you win $10,000!") 
elif revsd_rnd_num == user_num: 
    print("you guessed both digits but in reverse. you win $3,000!") 
elif rand_num_tens == user_num_tens: 
    print("you guessed one digit right. you win $1,000!") 
elif rand_num_tens == user_num_ones: 
    print("you guessed one digit right. you win $1,000!") 
elif rand_num_ones == user_num_tens: 
    print("you guessed one digit right. you win $1,000!") 
elif rand_num_ones == user_num_ones: 
    print("you guessed one digit right. you win $1,000!") 
else: 
    print("sorry, you didn't guess the right number.") 
相關問題