如何按原样比较两个两位整数,反转,一次一个字符

Posted

技术标签:

【中文标题】如何按原样比较两个两位整数,反转,一次一个字符【英文标题】:How to compare two two digit integers as is, reversed, and one character at a time 【发布时间】:2012-09-18 22:13:05 【问题描述】:

我是编程新手,正在尝试通过使用 python 参加介绍课程来学习它。

我的一项任务要求我们执行以下操作:

    将随机生成的两位整数与用户生成的两位整数进行比较。

      如果随机整数和用户生成的整数都匹配 print blah1 如果用户生成的整数与随机生成的整数的两位数相同,则打印 blah2 如果用户生成的整数有一位与随机生成的整数相同,则打印 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.")

【问题讨论】:

【参考方案1】:

这里有三个技巧可以帮助你:

    列表可以像字符串和数字一样进行比较。整数列表可以与另一个进行比较,如果包含的数字相同,则比较返回True

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

    您可以轻松反转列表。您可以使用reversed() 内置函数,也可以使用[start:stop:stride] 切片表示法给出步幅。后者也给你一个颠倒的列表:

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

    reversed() 函数返回一个迭代器,通过将它传递给 list() 构造函数,我们再次得到一个列表。

    您可以使用in 运算符来测试列表成员资格。使用它来测试单个整数是否是整数列表的一部分:

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

这 3 个技巧结合在一起应该为您提供重新编程脚本以处理整数所需的所有工具。将您的随机数和用户输入(使用 int() 函数转换为整数)存储在列表中,然后从那里开始。

如果您必须接受一个介于 10 和 99 之间的整数输入,事情会变得有点棘手。您可以使用 th 模 % 和除法 \ 运算符将 10 和 1 分开:

ones = number % 10
tens = number // 10

您可以使用divmod() function 组合这两个操作:

tens, ones = divmod(number, 10)

现在你又有了两个独立的整数来进行比较。

【讨论】:

Sameer 也可能会发现知道明显的[1,2]==reversed([2,1]) 给出False 很有帮助,您必须使用[1,2]==list(reversed([2,1])) 或切片符号来获取列表进行比较。顺便说一句,我不相信您使用reversed() 给出的示例的输出。 对,在 Python 3 中,reversed 是一个迭代器,我仍然经常使用 Python 2。我会纠正的。 reversed 在 Python 2 中也不会返回 listreverseiterator object 吗? @MartijnPieters:我不知道为什么我之前没有注意到整数除法和模数运算符。谢谢,差不多就行了。【参考方案2】:

我认为您面临的挑战是如何从两位整数中获取单独的数字。可以使用数学运算符。

想想像42 这样的数字中的每个数字代表什么。 4 是“十”位,2 是“个”位。反过来考虑可能会有所帮助。如果您在单独的变量中有整数 42,您将如何将它们组装成单个整数 42

希望我的提示能帮到你!如果我说的太迂回了,我可以试着详细说明一下,但我确实希望你能够自己解决它,而且我怀疑一旦你想到了合适的运算符,你就会很快得到它使用。

【讨论】:

【参考方案3】:

好的,谢谢大家的帮助。由于您的建议和提示,我设法按照规范完成了作业。

虽然,我对它的完成方式并不感到太自豪 - 对我来说似乎很草率 - 它有效。

这里是:

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

【讨论】:

以上是关于如何按原样比较两个两位整数,反转,一次一个字符的主要内容,如果未能解决你的问题,请参考以下文章

541-反转字符串 II

541-反转字符串 II

sql语句中怎么把字符串两位两位的反转

字符串比较未按预期工作。我想比较两个字符串值,但比较似乎总是给出一个真实的结果

541. 反转字符串 II

541. 反转字符串 II