Python-Übungsbeispiel 94

Python 100例Python 100 Beispiele

Problem:Zeitfunktionsbeispiel 4, ein Zahlenratespiel, das die Reaktionsgeschwindigkeit einer Person testet.

Programmanalyse:Keine.

Programmquellcode:

Beispiel (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': import time import random play_it = raw_input('Möchtest du spielen? (\'y\' oder \'n\')') while play_it == 'y': c = raw_input('Gib ein Zeichen ein:\n') i = random.randint(0,2**32) % 100 print 'Bitte gib die Zahl ein, die du rätst:\n' start = time.clock() a = time.time() guess = int(raw_input('Gib deine Vermutung ein:\n')) while guess != i: if guess > i: print 'Bitte gib eine etwas kleinere Zahl ein' guess = int(raw_input('Gib deine Vermutung ein:\n')) else: print 'Bitte gib eine etwas größere Zahl ein' guess = int(raw_input('Gib deine Vermutung ein:\n')) end = time.clock() b = time.time() var = (end - start) / 18.2 print var # print 'Du hast %6.3 Sekunden gebraucht' % time.difftime(b,a)) if var < 15: print 'Du bist sehr klug!' elif var < 25: print 'Du bist normal!' else: print 'Du bist dumm!' print 'Herzlichen Glückwunsch' print 'Die Zahl, die du geraten hast, ist %d' % i play_it = raw_input('Möchtest du es spielen?')

Die Ausgabe des obigen Beispiels ist:

do you want to play it.('y' or 'n')y
input a character:
5
please input number you guess:

input your guess:
60
……
please input a little bigger
input your guess:
29
please input a little smaller
input your guess:
28
3.81868131868e-05
you are very clever!
Congradulations
The number you guess is 28
do you want to play it.

Python 100例Python 100 Beispiele

Weitere Erweiterungen