4

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

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

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

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

3 回答 3

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 之间的整数输入,事情会变得有点棘手。%您可以使用 th 模和除法运算符将 10 和 1 分开\

ones = number % 10
tens = number // 10

divmod()您可以使用以下功能组合这两种操作:

tens, ones = divmod(number, 10)

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

于 2012-09-27T07:59:46.947 回答
0

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

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

这里是:

#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.")
于 2012-09-28T04:22:24.933 回答
0

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

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

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

于 2012-09-27T07:29:53.910 回答