不确定为什么会这样

时间:2014-11-24 19:41:30

标签: python python-3.4

好的,所以我写了这个,由于某种原因,乘数和最终得分并不总是有效:

Would you like to read the rules?(Y/N) n

Would you like to play?(Y/N) y

Lowest number: 1
Hightest number: 6

I'm thinking of a number guess what it is...

Guess: 3

Well Done! You guessed Correctly!

You scored 15 points!

Would you like to play again?[Y/N] n

You scored a total of 15 points!

Your score multipler is 1!

Your final score is 22!

现在您可以看到分数是正确的。乘数应为1.5,这意味着最终得分应为22.5。然而,它说乘数是1,最终得分是22?

但有时会这样:

Would you like to read the rules?(Y/N) n

Would you like to play?(Y/N) y

Lowest number: 1
Hightest number: 10

I'm thinking of a number guess what it is...

Guess: 5

Too low!

You guessed incorrectly. You have 2 live(s) remaining.

Guess: 7

Well Done! You guessed Correctly!

You scored 10 points!

Would you like to play again?[Y/N] n

You scored a total of 10 points!

Your score multipler is 2!

Your final score is 25!

正如您所看到的那样,范围是10,因此乘数应该是2.5,但是它将其打印为2.使用写入乘数,答案将是25但是它提供的答案应该是20。

我会将我的代码包含在下面,但这真的让我很困惑。我很快就需要帮助,因为明天我必须帮忙。请帮助我!!

#imports required modules
import random

#score set as global variable with the value 0
global score
score = 0

def start():
    #askes if user needs rules
    rules = input('Would you like to read the rules?(Y/N) ')
    #if yes rules are printed
    if rules.lower() == 'y':
        #prints inctructions 
        input('\t\t\t\t\tRules\n')
        input("Guess the number the computer is thinking of to win but you only have 3 lives.")
        input("\nStart by choosing the range of numbers the computer can choose from.")
        input("""For example if I wanted a number between 1 and 10 I would type 1 for
lowest number and 10 for highest number.""")
        input("""\nThe more lives you have left when you win the more points you get.
The larger the range of numbers you give the computer
the larger your score multipler.""")
        input("\nIt can be harder than you think!")
        input("\n\t\t\t\t\tEnjoy!")
    #if not player is aksed to play
    play = input("\nWould you like to play?(Y/N) ")
    if play.lower() == 'n':
        #if they don't game quits
        quit()

#main game code
def main():
    global low
    global up
    #generates number at random between the users two inputs
    comp_num = random.randint(low,up)
    #score set as global variable within function
    global score
    #lives created
    lives = 3
    while lives >= 1:
        #player guesses
        guess = int(input('Guess: '))
        if comp_num == guess:
            #if correct says well done
            print('\nWell Done! You guessed Correctly!')
            #1 live left player gets 5 points
            if lives == 1:
                score += 5
                print('\nYou scored 5 points!\n')
            #2 lives left player gets 10 points
            elif lives == 2:
                score += 10
                print('\nYou scored 10 points!\n')
            #3 lives left player gets 15 points
            elif lives == 3:
                score += 15
                print('\nYou scored 15 points!\n')
            break
        elif guess > up:
            #if number is high than possible player gets told
            print('\nThat is higher than the highest number you slected')
            #reminded of boundries
            print('\nRemember your lowest number is',low,'and your highest number is',up,'.')
            #asked to try again
            print('\nPlease try again!\n')
        elif comp_num >= guess:
            #if guess is too low tells player
            #one live taken for incorrect guess
            lives = lives -1
            print('\nToo low!\n')
            #player is told how many lives they have left
            print('You guessed incorrectly. You have',lives,'live(s) remaining.\n')
            if lives == 0:
                    #if player guesses incorrectly they get told the correct awnser
                    print('The number I was thinking of was...',comp_num,'!\n')
        elif comp_num <= guess:
            #if guess is too high tells player
            #one live taken for incorrect guess
            lives = lives -1
            print('\nToo high!\n')
            #player is told how many lives they have left
            print('You guessed incorrectly. You have',lives,'live(s) remaining.\n')
            if lives == 0:
                    #if player guesses incorrectly they get told the correct awnser
                    print('The number I was thinking of was...',comp_num,'!\n')

def end():
    #asks player if they want to play again
    play_again = input('Would you like to play again?[Y/N] ')
    while play_again.lower() == 'y':
        #if they do game resets and plays again
        if play_again.lower() == 'y':
            comp_num = random.randint(1,10)
            #starts game
            print('\nI\'m thinking of a number guess what it is...\n')
            main()
            play_again = input('Would you like to play again?[Y/N] ')
            if play_again.lower() == 'n':
                #if they don't want to play again while loop breaks
                break
    if play_again.lower() == 'n':
        #if they don't game ends
        #prints score
        input('\nYou scored a total of %i points!\n' % score)
        #prints mulipler
        input('Your score multipler is %i!\n' % multi)
        #calculates total score
        total = float(score*
                      multi)
        #prints total score
        input('Your final score is %i!\n' % total)
        input('Press enter to exit')

#calls intial game function to start
start()

#lower number set as global and value of user input
global low
low = int(input('\nLowest number: '))
#lower number set a global and value of user input
global up
up = int(input('Hightest number: '))

#sets up the score mulitpler
rang = float(up - low + 1)
multi = float(rang/4)
#if the multipler is less then 1 it gets sent to 1 so it doesn't remove points
if multi < 1:
    multi = 1

#starts atcual game
print('\nI\'m thinking of a number guess what it is...\n')

#calls main section of game
main()

#calls end of game to give option of playing again and resetting game
end()

1 个答案:

答案 0 :(得分:2)

检查一下:

    #if they don't game ends
    #prints score
    input('\nYou scored a total of %i points!\n' % score)
    #prints mulipler
    input('Your score multipler is %i!\n' % multi)
    #calculates total score
    total = float(score*
                  multi)
    #prints total score
    input('Your final score is %i!\n' % total)

您打印的数字如15,1,22。这些数字按其应计算,但不会按原样计算。这是因为您使用了错误的格式标识符。

将%i格式化程序替换为%f格式化程序,它将起作用。