Q1: week2-3, Ex-guess my number
在做week2练习:guess my number时遇到一个小问题。
代码已经写好,结果也和给出的答案一样,但是格式有一点不同,如下:
# 这是我的output
Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.
l
# 这是答案expected output
Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
可以发现区别在于答案的输入l是在“Enter ‘h’ to indicate the guess is too high. Enter ‘l’ to indicate the guess is too low. Enter ‘c’ to indicate I guessed correctly.”的同一行,而我的有一个换行,调整了半天也不知道如何让他们处于同一行,故请教大大们。我的代码如下,比较丑陋…
i = 50
low = 0
high = 99
print('Please think of a number between 0 and 100!')
print('Is your secret number: 50?')
while True:
print("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.")
a = input() # 问题出在这里,如何能将input和上面的print内容显示在一行里
if a != 'h' or 'l' or 'c':
print('Sorry, I did not understand your input.')
print('Is your secret number:' + str(i) + '?')
print("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.")
a = input()
elif a == 'l':
low = i
i = int((i+high)/2)
print('Is your secret number:' + str(i))
elif