我正在尝试创建游戏,无法使y / n函数正常工作
我尝试了下面的代码,它说y未定义,否则将跳过询问和打印(“放手!”)
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
// activate "test" profile
@ActiveProfiles("test")
// set custom config classes (don't forget Application)
@ContextConfiguration(classes = {TestConfig.class, Application.class})
public class MyControllerTest {
// define configuration for "test" profile (inline possible)
@Profile("test")
@Configuration
static class TestConfig {
@Bean
// !
@Primary
// I had an (auto configuration) exception/clash,
// when using *same bean name*, so *not* 'serviceB()', plz.
public ServiceB mockB() {
// prepare...
ServiceB mockService = Mockito.mock(ServiceB.class);
Mockito.when(mockService.greeting()).thenReturn("I am Mock Service B!");
// and return your mock object!
return mockService;
}
}
@Autowired
private MockMvc mvc;
@Test
public void testGreetingMock() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("A response: I am real ServiceA!, B response: I am Mock Service B!")));
}
}
这是它的外观:
import time
name = input ("Hello, what is your name?")
print ("Hello," +name)
time.sleep(1)
print ("ready to play? [y/n]")
y = print ("Let's go!")
我希望它等待输入y或n后才说(“走吧!”)
答案 0 :(得分:1)
您不是在要打印的“准备播放”行上要求输入,因此它不会等待。
答案 1 :(得分:0)
在打印提示符后,您没有等待命令。因此,它不会等待。
尝试使用此方法输入等待时间
answer = input("y/n?")
答案 2 :(得分:0)
首先,您需要将print ("ready to play? [y/n]")
更改为input("ready to play? [y/n]")
并将其放入变量中。接下来,删除y = print ("Let's go!")
,并对此进行更改(如果需要):
ready_status = input("are you ready? [y/n]")
if ready_status == 'y':
print("let's go!")
if ready_status == 'n':
print("...")
#do anything you want